php error

I am working with xampp and mysql , i have created there a table with columns but when i insert data it says : MySQL returned an empty result set (i.e. zero rows). (Query took 0.0021 seconds.) <?php // Include the database connection file include '../database.php'; if (isset($_POST["submit"])) { //validimi i te dhenave $amount = isset($_POST["amount"]) ? mysqli_real_escape_string($conn, $_POST["amount"]) : 0; $source = isset($_POST["source"]) ? mysqli_real_escape_string($conn, $_POST["source"]) : ''; $date = isset($_POST["date"]) ? mysqli_real_escape_string($conn, $_POST["date"]) : ''; $confirm = isset($_POST["confirm"]) ? 1 : 0; //Insertimi i te dhenave ne databaz $query = "INSERT INTO buxheti (amount, source,date, confirm) VALUES ('$amount', '$source','$date', '$confirm')"; if (mysqli_query($conn, $query)) { echo "<script>alert('Data inserted successfully');</script>"; } else { echo "<script>alert('Error: " . mysqli_error($conn) . "');</script>"; } } $query = "SELECT * FROM buxheti"; $result = mysqli_query($conn, $query); ?> i am sending data through a form
No description
149 Replies
anes039
anes039OP3w ago
form method is also POST the problem is about more when i click post the data should be inserted within the database,once i added date this issue occured
Jochem
Jochem3w ago
could be that '' isn't a valid date value '' !== NULL
ἔρως
ἔρως3w ago
an empty string isn't a valid date expression if you want an empty date, use '0000-00-00 00:00:00' or NULL the preferable option is to use NULL
anes039
anes039OP3w ago
now i created a new table without date and i cant even them anymore why is it the naming and all is good
ἔρως
ἔρως3w ago
what do you mean?
anes039
anes039OP3w ago
i cant insert data data it says : MySQL returned an empty result set (i.e. zero rows). (Query took 0.0021 seconds.) its showing this error if (isValid) { console.log(isValid); alert("Të dhënat u shtuan me sukses!"); addfundsform.submit(); // Dërgon formën manualisht } its showing error here in addFundsform ncaught TypeError: addfundsform.submit is not a function at HTMLFormElement.<anonymou i checked if i selected the element correctly and it is right
Chief
Chief3w ago
TypeError: addFundsform.submit is not a function maybe it's a js errorr
anes039
anes039OP3w ago
yes it is <div class="budget" id="budget"></div> <form method="POST" action="dashbord.php" id="add-funds-form"> <div class="form-buton-contener" id="form-buton-contener"> <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">
<label>Confirm</label> <input id="conf-input" name="confirm" type="checkbox" required> <button type="submit" id="submit-btn" name="submit">submit</button> </div> </form>
ἔρως
ἔρως3w ago
before running mysqli_query, add an echo $query and show the result
anes039
anes039OP3w ago
Warning: Undefined variable $query in C:\xampp\htdocs\ExpensesTracker\dashbord\dashbord.php on line 21 SELECT * FROM buxheti
ἔρως
ἔρως3w ago
you've put it in the wrong place
anes039
anes039OP3w ago
it is showing this query
ἔρως
ἔρως3w ago
^ before this: if (mysqli_query($conn, $query)) {
anes039
anes039OP3w ago
$query = "SELECT * FROM buxheti"; echo $query; $result = mysqli_query($conn, $query); like this
ἔρως
ἔρως3w ago
no
anes039
anes039OP3w ago
i did that and nothing is showing up echo $query; if (mysqli_query($conn, $query)) { echo "<script>alert('Data inserted successfully');</script>"; } else { echo "<script>alert('Error: " . mysqli_error($conn) . "');</script>"; } }
ἔρως
ἔρως3w ago
something has to show up remember that that only runs when you submit the form
anes039
anes039OP3w ago
i have to see in browser right? no i cant see anything
ἔρως
ἔρως3w ago
it just has to be somewhere in the output
Jochem
Jochem3w ago
did you fix this?
anes039
anes039OP3w ago
yes i changed the name of button
Jochem
Jochem3w ago
and you're seeing the POST in the network tab now? have you checked your PHP error logs?
anes039
anes039OP3w ago
wait how do i do that because i tried to echo my query but i dont see it anywhere once i submit the form the form is closed
Jochem
Jochem3w ago
depends on your server setup
anes039
anes039OP3w ago
i am using xampp
Jochem
Jochem3w ago
it should have an option in xammps context menu iirc
anes039
anes039OP3w ago
yea i think it is gone because i got 200 but in database after i submit the form i got this MySQL returned an empty result set (i.e. zero rows). (Query took 0.0004 seconds.)
Jochem
Jochem3w ago
otherwise, check the xampp/php/logs/ and xampp/apache/logs/ folders
anes039
anes039OP3w ago
i dont have logs in apache yes but it doesnt say anything about erro i think
Jochem
Jochem3w ago
in some setups, apache's error logs are used as PHP error logs I think? it's also been a good long while since I used xampp
Jochem
Jochem3w ago
if it's not printing out your query, it's more likely that something is going wrong with your PHP before it's getting to MySQL, so your mysql logs aren't really useful in this case. try adding this just before if (mysqli_query($conn, $query)) { and moving it up until you do see it:
echo "Got to here.";
exit;
echo "Got to here.";
exit;
ἔρως
ἔρως3w ago
or use die which does both the echo and exit
Jochem
Jochem3w ago
even better, yeah. I don't know why I always forget about die()
ἔρως
ἔρως3w ago
because it's very ... authoritive
Jochem
Jochem3w ago
die("Got to here.");
die("Got to here.");
ἔρως
ἔρως3w ago
in this case die($query);
Jochem
Jochem3w ago
no, cause it's not printing the query, and we're moving it up to see where the error is, cause they either can't find the error logs or have them turned off at best die("Query: $query"); so there's at least some output if the query hasn't been defined
ἔρως
ἔρως3w ago
i know, but die doesn't show in the logs the idea is to see if something happens or not for the query
Jochem
Jochem3w ago
a warning or error that ceases execution will
ἔρως
ἔρως3w ago
if the site functions normally, the code isn't even being executed
Jochem
Jochem3w ago
(also, I have been writing too much JS, and keep forgetting semicolons x_x)
ἔρως
ἔρως3w ago
the chunk there only runs when you submit a post request if, when testing, the request is submitted and everything keeps working normally, then this isn't the right code at all or the isset has the wrong variable
anes039
anes039OP3w ago
let me try that
anes039
anes039OP3w ago
No description
anes039
anes039OP3w ago
i should see here for example dashboard.php right? after i submit the form
anes039
anes039OP3w ago
No description
anes039
anes039OP3w ago
<?php // Include the database connection file include '../database.php'; if (isset($_POST["submit"])) { //validimi i te dhenave $amount = isset($_POST["Amount"]) ? mysqli_real_escape_string($conn, $_POST["Amount"]) : 0; $source = isset($_POST["Source"]) ? mysqli_real_escape_string($conn, $_POST["Source"]) : ''; $confirm = isset($_POST["Confirm"]) ? 1 : 0; //Insertimi i te dhenave ne databaz
$query = "INSERT INTO buxheti (Amount, Source, Confirm) VALUES ('$amount', '$source', '$confirm')"; echo $query;
echo "Got to here."; die($query); if (mysqli_query($conn, $query)) { echo "<script>alert('Data inserted successfully');</script>"; } else { echo "<script>alert('Error: " . mysqli_error($conn) . "');</script>"; } } $query = "SELECT * FROM buxheti"; $result = mysqli_query($conn, $query); ?>
Jochem
Jochem3w ago
did you open the dev tools before clicking submit?
anes039
anes039OP3w ago
yes but i got the data though i just switched to all to see all my files and you know because it was fetch before
ἔρως
ἔρως3w ago
you see, the submit is missing
anes039
anes039OP3w ago
where
ἔρως
ἔρως3w ago
you sent the form with form.submit() instead of clicking the button
anes039
anes039OP3w ago
so i should select the button
ἔρως
ἔρως3w ago
no
Jochem
Jochem3w ago
ooo, good catch!
anes039
anes039OP3w ago
and then do .submit i hate php hahha
ἔρως
ἔρως3w ago
this has nothing to do with php it's html and js
anes039
anes039OP3w ago
okay but where tell me i dont see it
ἔρως
ἔρως3w ago
just add an hidden field with the name submit then remove the name from the button it's terrible, but since you're using .submit(), it's the easiest way
anes039
anes039OP3w ago
okay
Jochem
Jochem3w ago
basically, HTML only adds the submit field to the post request if the submit happens by pressing submit. If you intercept it, it doesn't add it in that's so that you can have multiple submit buttons with different names, and detect which was pressed on the backend without any frontend code
ἔρως
ἔρως3w ago
yup
anes039
anes039OP3w ago
i just removed the name property entirely but now still the same issue with database shit
ἔρως
ἔρως3w ago
and did you add the hidden field?
anes039
anes039OP3w ago
yes <input type="hidden" name="submit" value="1"> i got the same error as before with this dashbord.js:104 Uncaught TypeError: addfundsform.submit is not a function at HTMLFormElement.<anonymous> (dashbord.js:104:26)
ἔρως
ἔρως3w ago
does addfundsform exist?
anes039
anes039OP3w ago
const addfundsform = document.getElementById('add-funds-form'); yes and also the naming is correct
Jochem
Jochem3w ago
what element is that ID on? Is it only one one element?
anes039
anes039OP3w ago
yes
Jochem
Jochem3w ago
does console.log(addfundsform) after that line output something sensible?
anes039
anes039OP3w ago
wait yes the form itself
Jochem
Jochem3w ago
can you share your HTML and JS? Cause this isn't a PHP issue and we're groping around in the dark without the code
ἔρως
ἔρως3w ago
i don't see anything wrong
Jochem
Jochem3w ago
wow, that was a sneaky one the button, and later the hidden input, were overwriting the addfundsform.submit property you can't use the name submit on a form property and still use the .submit() function, because the field will overwrite the built-in function rename the hidden field to addfunds and check for isset($_POST['addfunds']) instead of isset($_POST['submit']) (also, obligatory "use prepared statements")
ἔρως
ἔρως3w ago
that is so stupid and makes so much sense
Jochem
Jochem3w ago
((and I'd recommend trying to be more consistent in your code. You've got lots of typos you just ran with in your variable names, lots of little language errors that are inconsistently used (Amount in displayed language, and amaunt in code.) Your errors are also in one language, while the interface is in another. Pick one language and use it well, cause right now this looks like a mess to any potential senior dev involved in a hiring process)) I generally go for English in the code including comments because that's what the programming language is written in, and then my native Dutch in the interface language cause that's what my users use. Whatever you pick, it's important for code quality to be consistent and predictable though @anes039 did that work?
anes039
anes039OP3w ago
I am not home And yeah about the naming Thats a group project so …. Thanks for the tips though I will try it
Jochem
Jochem3w ago
it's even more important in a group project like... when I look at code to judge its quality, the first thing I look at is consistent indentation, the second thing is consistent and sensible naming those are literally the bare minimum. If those aren't impeccable, the entire resume is going straight in the bin
anes039
anes039OP3w ago
Yes i agree we should work into that it is still not working 0 rows nevermind i will try redo it from the beegining
Jochem
Jochem3w ago
are you still getting the javascript error?
anes039
anes039OP3w ago
noo
ἔρως
ἔρως3w ago
can you show the code? also, why don't you use ajax?
anes039
anes039OP3w ago
because it one of the projects goals to use php
ἔρως
ἔρως3w ago
uh ... what?
anes039
anes039OP3w ago
the professor set as a condition for database to use php and xamp for front we use html css and js now i am not getting the submit error in njs but still 0 rows inserted in database
ἔρως
ἔρως3w ago
and how would that invalidate what i asked?
anes039
anes039OP3w ago
he said dont use libraries or fetch
ἔρως
ἔρως3w ago
that's dumb as hell
anes039
anes039OP3w ago
yeaa
ἔρως
ἔρως3w ago
but yeah, that invalidates what i said
anes039
anes039OP3w ago
i dont even why these data are not being inserted because i receive them in the network this was working so well till yesterday
ἔρως
ἔρως3w ago
the names of the columns in the database start with capital leters
anes039
anes039OP3w ago
okay i will change in my code the naming nope
ἔρως
ἔρως3w ago
add a var_dump($_POST); before the if
anes039
anes039OP3w ago
okay then?
ἔρως
ἔρως3w ago
then run it if you don't see anything different, then you are missing something
anes039
anes039OP3w ago
still the same
ἔρως
ἔρως3w ago
are you sure that it is submitting to that file?
anes039
anes039OP3w ago
ἔρως
ἔρως3w ago
that's in your pc, i can't see it
anes039
anes039OP3w ago
i know but i think its right
ἔρως
ἔρως3w ago
is this on github?
anes039
anes039OP3w ago
we worked on develop branch
anes039
anes039OP3w ago
i think we did not push it yet never mind mate leave it i will try to fix it thanks for the effort
ἔρως
ἔρως3w ago
alright it's very hard to help without the actual code and seeing it but you're welcome
anes039
anes039OP3w ago
Can u dm me? @ἔρως
ἔρως
ἔρως3w ago
no, i've set myself to do not accept private messages from this server precisely to prevent people from messaging me to ask for help nothing against you and don't take it personally, but, you aren't the first person i said "no" to, and you won't be the last one it's nearly impossible to relax when people then get used to just send private messages to ask for help, instead of using the help channels so, i prevent that from happening from the start - even if some people get angry because of that
Jochem
Jochem3w ago
not to mention that a good chunk of why these help channels exist, is so that your question can help multiple people in the future. Keeping things in open chat also makes it so multiple folks can help, and it emphasizes that it's a bad idea to share credentials and stuff like that with effective strangers
ἔρως
ἔρως3w ago
yeah, sending credentials to a random person because that person offered to help is the easiest way to get your stuff hacked and ruined
anes039
anes039OP3w ago
yes you are right idk what to look up anymore when i do echo on my dashboard.php after my form submission i see no logs and also i matched the columns names with the one in my php file
Jochem
Jochem3w ago
are you getting javascript errors still? Do you see a POST to dashboard.php? can you share your updated code?
anes039
anes039OP3w ago
when i do dev tools with 4g i see the request and data but that data is not being inserted into database
Jochem
Jochem3w ago
you removed the hidden field
anes039
anes039OP3w ago
yes but i dont think that is the issue
Jochem
Jochem3w ago
That doesn't solve this problem I'm telling you it is
anes039
anes039OP3w ago
okay so i should
ἔρως
ἔρως3w ago
it is
anes039
anes039OP3w ago
add again that field
ἔρως
ἔρως3w ago
yes, but with a different name something like "sent"
anes039
anes039OP3w ago
<input type="hidden" name="sent" /> like this
Jochem
Jochem3w ago
This would be my recommendation
ἔρως
ἔρως3w ago
by the way, you NEED to do server-side validation too
Jochem
Jochem3w ago
primarily because there's other forms, and it looks like you'll want to be able to distinguish which one is being used
anes039
anes039OP3w ago
it workedddd yeaaaa thanks guys for all of this hahahaha what was the issue can u guys tell me again because i dont wanna make the same mistake and can i add now a new column with date
Jochem
Jochem3w ago
there were a couple. First, calling the field "submit" overwrote the submit method on the form element object, so your javascript wasn't actually sending the data to the backend. The second issue is that when you use .submit, it doesn't include the button you clicked to submit, so the value of the button was never getting set in the data. when you renamed the button to btn-submit, you solved the first issue, but because you were still programmatically submitting the form rather than relying on HTML to do it naturally, you were clearing the btn-submit value from the POST data. That's why Epic suggested adding the hidden field and checking for that instead and yeah, you can add the date column now too if you want
anes039
anes039OP3w ago
okay thanks one more question can i do also here the logic for add item in the same file because the form of add item is also here
ἔρως
ἔρως3w ago
you can but it's best to separate things, but you can
anes039
anes039OP3w ago
thanks again guys peace
ἔρως
ἔρως3w ago
by the way, for dates, you MUST DO SERVER-SIDE VALIDATION
anes039
anes039OP3w ago
okay
Jochem
Jochem3w ago
for everything you must do server-side validation
ἔρως
ἔρως3w ago
the format of the date won't match the format of the input
anes039
anes039OP3w ago
what data type should i use varchar?
Jochem
Jochem3w ago
client-side validation is for user convenience only, serverside validation is for security DATE
ἔρως
ἔρως3w ago
varchar is for small strings, like usernames and emails and HASHED passwords
anes039
anes039OP3w ago
<form method="POST" action="./dashbord.php" id="Withdraw-form"> <div class="Withdraw-contener" id="Withdraw-contener"> <input type="hidden" name="addItem"/> <i class="fa-solid fa-x" id="WithdrawDelet"></i> <label>Amount</label> <input id="amaunt-input" name ="amount"type="number" min="1" required> <label>Description</label> <input id="Description-input" name="Description" type="text"> <label>Date</label> <input id="date-input" name="date"type="date" required> <label>Confirm</label> <input id="conf-input" name="confirm"type="checkbox" required> <button type="submit">submit</button> </div> </form> </div> <?php include '../database.php'; if (isset($_POST["addfunds"])) { // Sanitize and validate inputs $amount = mysqli_real_escape_string($conn, $_POST["amount"]); $source = mysqli_real_escape_string($conn, $_POST["source"]); $confirm = isset($_POST["confirm"]) ? 1 : 0; // Handle checkbox as boolean $date = mysqli_real_escape_string($conn, $_POST["date"]); $query = "INSERT INTO budget (Amount, Source, Confirm, Date) VALUES (?, ?, ?, ?)"; $stmt = $conn->prepare($query); if ($stmt) {
$stmt->bind_param("diss", $amount, $source, $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>"; } } if (isset($_POST["addItem"])) { // Sanitize and validate inputs $amount = mysqli_real_escape_string($conn, $_POST["amount"]); $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>"; } } ?> can i do like this? for the item table
Jochem
Jochem3w ago
please use code blocks, this is borderline unreadable without formatting #how-to-ask-good-questions has some instructions on how to use them
ἔρως
ἔρως3w ago
you have the field <input type="hidden" name="addItem"/> but you are looking for if (isset($_POST["addfunds"])) { that won't work wrong name and no value fix the hidden field or, you can use a name like addfunds[amount], which creates an array
anes039
anes039OP3w ago
Yea but look thats a seperated form That i send In message.txt And thats why i changed the naming And added another if
ἔρως
ἔρως3w ago
same concept applies
anes039
anes039OP3w ago
So i can keave The same naming yes it did thanks so much
ἔρως
ἔρως3w ago
you're welcome

Did you find this page helpful?