date validation

when i insert the in database sometimes i got 00-00-0000
65 Replies
anes039
anes039OP4w ago
i am trying to not allow the user to do this because you cant insert 00-00-0000 thats impossible
Jochem
Jochem4w ago
split the string using explode, then use checkdate
anes039
anes039OP4w ago
okay it is asking for two arguments the checkdate i should pass the reformattedDate and?
Jochem
Jochem4w ago
if you look at the link, it tells you exactly how to use the function
anes039
anes039OP4w ago
what do u think now it says again invalid date format
Jochem
Jochem4w ago
sorry, I'm not going digging through 200 lines of HTML, JS, and PHP code to find what you changed
anes039
anes039OP4w ago
no no just the begineging you have only the first part because i could not send it seperated dont go through html
Jochem
Jochem4w ago
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 data
anes039
anes039OP4w ago
i 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
Jochem
Jochem4w ago
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?
anes039
anes039OP4w ago
i changed it now ven even for the source the data is not being inserted in my database its varchar
Jochem
Jochem4w ago
sorry, I have no idea what you're asking right now
anes039
anes039OP4w ago
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
Jochem
Jochem4w ago
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 itself
anes039
anes039OP4w ago
and 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
Jochem
Jochem4w ago
in this snippet:
<div class="overlay" id="outlay"></div>
<form method="POST" action="./dashbord.php" id="add-funds-form">
<div class="form-buton-contener" id="form-buton-contener">
<input type="hidden" name="addfunds"/>
<i class="fa-solid fa-x" id="add-funds-delet"></i>
<label class="amount">Amount</label>
<input id="amaunt-input" name="amount" type="number" min="1" required>
<label class="source">Source</label>
<input id="sourece-input" name="source" type="text" required>
<label>Date</label>
<input id="date-input" name="date" type="date">
<label>Confirm</label>
<input id="conf-input" name="confirm" type="checkbox">
<button type="submit" name="btn-submit">Submit</button>
</div>
</form>


<form method="POST" action="./dashbord.php" id="Withdraw-form">
<div class="Withdraw-contener" id="Withdraw-contener">
<input type="hidden" name="addfunds" />
<i class="fa-solid fa-x" id="WithdrawDelet"></i>
<label>Amount</label>
<input id="amaunt-input" name ="amount"type="number" min="1" required>
<div class="overlay" id="outlay"></div>
<form method="POST" action="./dashbord.php" id="add-funds-form">
<div class="form-buton-contener" id="form-buton-contener">
<input type="hidden" name="addfunds"/>
<i class="fa-solid fa-x" id="add-funds-delet"></i>
<label class="amount">Amount</label>
<input id="amaunt-input" name="amount" type="number" min="1" required>
<label class="source">Source</label>
<input id="sourece-input" name="source" type="text" required>
<label>Date</label>
<input id="date-input" name="date" type="date">
<label>Confirm</label>
<input id="conf-input" name="confirm" type="checkbox">
<button type="submit" name="btn-submit">Submit</button>
</div>
</form>


<form method="POST" action="./dashbord.php" id="Withdraw-form">
<div class="Withdraw-contener" id="Withdraw-contener">
<input type="hidden" name="addfunds" />
<i class="fa-solid fa-x" id="WithdrawDelet"></i>
<label>Amount</label>
<input id="amaunt-input" name ="amount"type="number" min="1" required>
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
anes039
anes039OP4w ago
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, 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>"; } }
Jochem
Jochem4w ago
what errors?
anes039
anes039OP4w ago
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
Jochem
Jochem4w ago
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?
anes039
anes039OP4w ago
noo i did with echo i think yes it has value
Jochem
Jochem4w ago
echo works too, but you gotta run through those steps and include those results when you ask further questions
anes039
anes039OP4w ago
so what is the next step now
Jochem
Jochem4w ago
what column type is the Description column?
anes039
anes039OP4w ago
varchar
Jochem
Jochem4w ago
oh! Why are you using diss as the types?
anes039
anes039OP4w ago
idk chatgpt did it hahaha
Jochem
Jochem4w ago
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 store
anes039
anes039OP4w ago
and what should i write
Jochem
Jochem4w ago
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?
ἔρως
ἔρως4w ago
have you considered swapping from mysqli to pdo? also, why are you double escaping? in pdo, you simply do like this:
<?php

$pdo = new PDO('mysql:...');
$stmt = $pdo->prepare('query goes in here');
$stmt->execute([$_POST['amount'], ...]);
<?php

$pdo = new PDO('mysql:...');
$stmt = $pdo->prepare('query goes in here');
$stmt->execute([$_POST['amount'], ...]);
ἔρως
ἔρως4w ago
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 invalid
Jochem
Jochem4w ago
That always feels to magic for me
ἔρως
ἔρως4w ago
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 valid
Jochem
Jochem4w ago
I guess, but if you know the format that's incoming, you can validate is as that format and nothing else
ἔρως
ἔρως4w ago
but mysql only supports 2025-01-01
Jochem
Jochem4w ago
but it's a preference, really
ἔρως
ἔρως4w ago
true, that is a good point but is the value consistent in all browsers?
Jochem
Jochem4w ago
MDN says it's consistently YYYY-MM-DD, regardless of the display value
ἔρως
ἔρως4w ago
that is good, but it is missing the time i think mysql handles it by adding 00:00:00
Jochem
Jochem4w ago
depends on if you use the DATE or DATETIME type
ἔρως
ἔρως4w ago
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
anes039
anes039OP3w ago
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
ἔρως
ἔρως3w ago
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
anes039
anes039OP3w ago
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>";
}
ἔρως
ἔρως3w ago
what i think of it? my eyes hurt trying to read this mess
anes039
anes039OP3w ago
AHAHAHA
ἔρως
ἔρως3w ago
no, seriously this is very very hard to read
anes039
anes039OP3w ago
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
ἔρως
ἔρως3w ago
i don't see anything wrong there
anes039
anes039OP3w ago
$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
ἔρως
ἔρως3w ago
and what's the problem?
anes039
anes039OP3w ago
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
Jochem
Jochem3w ago
@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
ἔρως
ἔρως3w ago
which while?
anes039
anes039OP3w ago
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
Jochem
Jochem3w ago
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 twice
anes039
anes039OP3w ago
so does it mean if i delete the first while it will work
Jochem
Jochem3w ago
give it a shot?
anes039
anes039OP3w ago
yess its showing thanks so much i should look more about this internal pointer i was afraid about something the variable scope
Jochem
Jochem3w ago
if you read the docs for mysqli_fetch_assoc, it should be in there
anes039
anes039OP3w ago
guys

Did you find this page helpful?