date validation
when i insert the in database sometimes i got 00-00-0000
65 Replies
i am trying to not allow the user to do this
because you cant insert 00-00-0000
thats impossible
okay
it is asking for two arguments
the checkdate
i should pass the reformattedDate
and?
if you look at the link, it tells you exactly how to use the function
what do u think now
it says again invalid date format
sorry, I'm not going digging through 200 lines of HTML, JS, and PHP code to find what you changed
no no
just the begineging
you have only the first part
because i could not send it seperated
dont go through html
you can just copy paste code snippets, you don't have to upload the entire file...
Also, you're using
return
outside of a function. If you want to stop execution of a running script outside of a function, you should use exit
.
Anyway, I don't see anything immediately wrong
it really depends on what you're feeding into the datai am using date ..... in html form
now there is another issue
when i add anything to the first table the same is added to the second
Warning: Undefined array key "description" in C:\xampp\htdocs\ExpensesTracker\dashbord\dashbord.php on line 32
that's because you're using
addfunds
for both blocks
how is it supposed to be able to tell which bit of code to run?i changed it
now ven
even
for the source the data is not being inserted
in my database its varchar
sorry, I have no idea what you're asking right now
the source variable is not being inserted into the database
it is a property for the addFunds
inside the form
and that string is not being
inserted
then check in the network tab if it's being sent to the server, and check on the server if it's being received
Also, I just noticed this, but you're not using
<label>
correctly. You either have to use the for
attribute to point to the ID of the labeled element, or wrap the labeled element inside the label tag itselfand also when i change the name to the addItem of the hidden input it is not inserting data anymore in item table
why is that
in this snippet:
you have two hidden fields called addfunds in two different forms
you want to run different code when one form is submitted, than when the other is submitted
so the hidden inputs should have different names, so that you can detect on the backend which form was used
i fixed the table issue but now with the description i am checking there were a few errors in js
it is inserting 0
instead of the string
if (isset($_POST["addItem"])) {
// Sanitize and validate inputs
$amount = mysqli_real_escape_string($conn, $_POST["amount"]);
$description = isset($POST["description"]) ? mysqli_real_escape_string($conn, $_POST["description"]) : '';
$confirm = isset($_POST["confirm"]) ? 1 : 0; // Handle checkbox as boolean
$date = mysqli_real_escape_string($conn, $_POST["date"]);
$query = "INSERT INTO items (Amount,
$stmt->bind_param("diss", $amount, $description, $confirm, $date); if ($stmt->execute()) { echo "<script> alert('Data Inserted Successfully'); </script>"; } else { echo "<script> alert('Error: " . $stmt->error . "'); </script>"; } $stmt->close(); } else { echo "<script> alert('Error preparing statement: " . $conn->error . "'); </script>"; } }
Description
, Confirm, Date
) VALUES (?, ?, ?, ?)";
$stmt = $conn->prepare($query);
if ($stmt) {
$stmt->bind_param("diss", $amount, $description, $confirm, $date); if ($stmt->execute()) { echo "<script> alert('Data Inserted Successfully'); </script>"; } else { echo "<script> alert('Error: " . $stmt->error . "'); </script>"; } $stmt->close(); } else { echo "<script> alert('Error preparing statement: " . $conn->error . "'); </script>"; } }
what errors?
the data was not being received because of the incorrect element selector
$stmt->bind_param("diss", $amount, $description, $confirm, $date);
maybe this line is the issue
because description is string
what is the role of bind.param
or can we do typecasting before description
because its returning 0
in database
PHP is very forgiving with automatic type casting. a string "0" will count as the int 0 as well for example
have you tried using
var_dump
or die
to inspect what $description
actually has as a value?noo
i did with echo i think
yes
it has value
echo works too, but you gotta run through those steps and include those results when you ask further questions
so what is the next
step now
what column type is the Description column?
varchar
oh! Why are you using
diss
as the types?idk
chatgpt did it
hahaha
don't use ChatGPT if you don't know how to do it without ChatGPT
change
diss
to match the actual types of the data you want to storeand what should i write
read the documentation for bind_param on php.net and try to figure that one out yourself
programming is primarily understanding what you're asking the computer to do, and the only way you can do that is by reading the documentation
and seriously, don't use ChatGPT. Aside from the ethical questions about the data it's been trained on, it produces garbage code that may or may not work, and if you're using it to learn, it'll just help you get more stuck. If you'd written this code yourself, you would've learned something about reading documentation, and running through code in your head to figure out how to get a computer to do what you want and to figure out how to debug. Instead, you copy pasted a bunch of code back and forth and when ChatGPT couldn't manage to fix its own garbage code, you came here...
What are you going to do at a real job when you graduate, when CoPilot isn't allowed, because there's still no settled case law on whether or not AI generated code can be copyrighted? Or whether the copyright of generated code can lie with other third parties, because it's been generated based solely on someone else's IP?
have you considered swapping from mysqli to pdo?
also, why are you double escaping?
in pdo, you simply do like this:
that explains it
by the way, to validate a date, just do
new DateTime('date comes here');
catch the exception that it throws when the date is invalidThat always feels to magic for me
then format it with the
format
method
you will have to do this anyways, either manually or automatically
because a date of 01/01/2025
is validI guess, but if you know the format that's incoming, you can validate is as that format and nothing else
but mysql only supports
2025-01-01
but it's a preference, really
true, that is a good point
but is the value consistent in all browsers?
MDN says it's consistently YYYY-MM-DD, regardless of the display value
that is good, but it is missing the time
i think mysql handles it by adding 00:00:00
depends on if you use the DATE or DATETIME type
but hey, learning the datetime class is very important
i think the field is in date, from what i remember, so, no hours added
but seriously, that code is a mess, and swapping to pdo will make many things a lot easier
and when you try to use more than one prepared statement at the same time with mysqli... oh boy
if you dont do all the steps correctly, you will get deadlocks and out of sync errors
i fixed it
but there is another issue
now when i connect the primary key of the user into budgettable the foreign key is null
if i insert it automatically it matches
$userBudgetQuery = "SELECT users.id, users.fullname, budget.Amount, budget.Source, budget.Confirm, budget.Date
FROM users
inner JOIN budget ON users.id = budget.user";
when i add the budget should that budget table itself find x user which logged in and has that budget
and also
even though i delete my budget items
the id is not starting from 0
why is that
it never starts from 0
auto-increment primary keys start at 1
foreign keys can't be auto-increment, so, you have to set the value to it when inserting
okay
one question i want to ask u
so in my case
we have an application for expenses tracker
i should insert the foreign key in my db
right?
when i add the budget
and another question
<div class="balanci">
<span>balanci</span>
<?php
while($row = mysqli_fetch_assoc($rez)){
?>
<span> <?php echo $row['Amount'] ?> </span>
<?php
}
?>
</div>
what do u think about this
because i am trying to get the budget value
is the syntax right
$userBudgetQuery = "SELECT users.id, users.fullname, budget.Amount, budget.Source, budget.Confirm, budget.Date
FROM users
inner JOIN budget ON users.id = budget.user";
$rez = mysqli_query($conn, $userBudgetQuery); if (!$rez) { die("Query failed: " . mysqli_error($conn)); } while ($row = mysqli_fetch_assoc($rez)) { echo "User ID: " . $row['id'] . " - Name: " . $row['fullname'] . " - Budget : " . $row['Amount'] . " - Source: " . $row['Source'] . " - Confirm: " . $row['Confirm'] . " - Date: " . $row['Date'] . "<br>"; } if (mysqli_num_rows($rez) > 0) { while ($row = mysqli_fetch_assoc($rez)) { echo "<pre>"; print_r($row);
echo "</pre>"; echo "User ID: " . $row['id'] . " - Budget Amount: " . $row['Amount'] . "<br>"; } } else { echo "No results found for the query.<br>";
}
$rez = mysqli_query($conn, $userBudgetQuery); if (!$rez) { die("Query failed: " . mysqli_error($conn)); } while ($row = mysqli_fetch_assoc($rez)) { echo "User ID: " . $row['id'] . " - Name: " . $row['fullname'] . " - Budget : " . $row['Amount'] . " - Source: " . $row['Source'] . " - Confirm: " . $row['Confirm'] . " - Date: " . $row['Date'] . "<br>"; } if (mysqli_num_rows($rez) > 0) { while ($row = mysqli_fetch_assoc($rez)) { echo "<pre>"; print_r($row);
echo "</pre>"; echo "User ID: " . $row['id'] . " - Budget Amount: " . $row['Amount'] . "<br>"; } } else { echo "No results found for the query.<br>";
}
what i think of it? my eyes hurt trying to read this mess
AHAHAHA
no, seriously
this is very very hard to read
okay my bad
<div class="balanci">
<span> balanci</span>
<?php
while($row = mysqli_fetch_assoc($rez)){
?>
<span> <?php echo $row['Amount'] ?> hi</span>
<?php
}
?>
</div>
this is the part
i was asking
i don't see anything wrong there
$rez = mysqli_query($conn, $userBudgetQuery);
if (!$rez) {
die("Query failed: " . mysqli_error($conn));
}
while ($row = mysqli_fetch_assoc($rez)) {
echo "User ID: " . $row['id'] . " - Name: " . $row['fullname'] . " - Budget : " . $row['Amount'] . " - Source: " . $row['Source'] . " - Confirm: " . $row['Confirm'] . " - Date: " . $row['Date'] . "<br>";
}
if (mysqli_num_rows($rez) > 0) {
while ($row = mysqli_fetch_assoc($rez)) {
echo "<pre>";
print_r($row);
echo "</pre>"; echo "User ID: " . $row['id'] . " - Budget Amount: " . $row['Amount'] . "<br>"; } } else { echo "No results found for the query.<br>";
} when i do it right its showing a result within the while
echo "</pre>"; echo "User ID: " . $row['id'] . " - Budget Amount: " . $row['Amount'] . "<br>"; } } else { echo "No results found for the query.<br>";
} when i do it right its showing a result within the while
and what's the problem?
i tried to log the $row in html code and its showing nothing , because i want to get that amount to show near balance
with $rez its showing an error object of class cannot be converted to string
when i do the while loop
it shows nothing
@anes039 can you please at least use a syntax highlighting code block when you share code? There's instructions in #how-to-ask-good-questions
which while?
at balanci div
You are connectedUser ID: 9 - Name: AnesBaba - Budget : 1 - Source: rroga - Confirm: 1 - Date: 2025-01-13
i want to pickup budget and display it in span element near balanci
codepen is pretty pointless for PHP code. You can share your code with syntax highlighting by starting a new line with ```php, pasting your code below that line, and then on a final new line all the way at the bottom add ```. That's the backtick, the key top left, next to 1 on a QWERTY keyboard
replace PHP with any other language if you share other code, like
html
or js
or css
without looking at the new code though, just the pasted one a few mesages above, you seem to be using the same $rez twice, once in the echo, then once again after you check if there's rows? $rez has an internal pointer that you'd need to reset to use it twiceso does it mean if i delete the first while it will work
give it a shot?
yess
its showing
thanks so much
i should look more
about this
internal pointer
i was afraid about something the variable scope
if you read the docs for mysqli_fetch_assoc, it should be in there
guys