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 form149 Replies
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
could be that '' isn't a valid date value
'' !== NULL
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
now
i created a new table without date
and i cant even
them anymore
why is it
the naming and all is good
what do you mean?
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
TypeError: addFundsform.submit is not a function
maybe it's a js errorr
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>
<label>Confirm</label> <input id="conf-input" name="confirm" type="checkbox" required> <button type="submit" id="submit-btn" name="submit">submit</button> </div> </form>
before running
mysqli_query
, add an echo $query
and show the resultWarning: Undefined variable $query in C:\xampp\htdocs\ExpensesTracker\dashbord\dashbord.php on line 21
SELECT * FROM buxheti
you've put it in the wrong place
it is showing
this query
^
before this:
if (mysqli_query($conn, $query)) {
$query = "SELECT * FROM buxheti";
echo $query;
$result = mysqli_query($conn, $query);
like this
no
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>";
}
}
something has to show up
remember that that only runs when you submit the form
i have
to see in browser
right?
no i cant see anything
it just has to be somewhere in the output
did you fix this?
yes
i changed the name of button
and you're seeing the POST in the network tab now?
have you checked your PHP error logs?
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
depends on your server setup
i am using
xampp
it should have an option in xammps context menu iirc
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.)
otherwise, check the xampp/php/logs/ and xampp/apache/logs/ folders
i dont have logs
in apache yes
but it doesnt say anything about erro i think
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
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:
or use
die
which does both the echo and exiteven better, yeah. I don't know why I always forget about
die()
because it's very ... authoritive
in this case
die($query);
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 definedi know, but
die
doesn't show in the logs
the idea is to see if something happens or not for the querya warning or error that ceases execution will
if the site functions normally, the code isn't even being executed
(also, I have been writing too much JS, and keep forgetting semicolons x_x)
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 variablelet me try
that
i should see here for example dashboard.php
right?
after i submit the form
<?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); ?>
$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); ?>
did you open the dev tools before clicking submit?
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
you see, the
submit
is missingwhere
you sent the form with
form.submit()
instead of clicking the buttonso i should select the button
no
ooo, good catch!
and then do .submit
i hate php hahha
this has nothing to do with php
it's html and js
okay but where
tell me
i dont see it
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 wayokay
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
yup
i just removed the name property entirely
but now still the same issue with database
shit
and did you add the hidden field?
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)
does
addfundsform
exist?const addfundsform = document.getElementById('add-funds-form');
yes
and also
the naming is correct
what element is that ID on? Is it only one one element?
yes
does console.log(addfundsform) after that line output something sensible?
wait
yes
the form
itself
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
i don't see anything wrong
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")that is so stupid and makes so much sense
((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?
I am not home
And yeah about the naming
Thats a group project so ….
Thanks for the tips though
I will try it
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
Yes i agree we should work into that
it is still not working
0 rows
nevermind i will try redo it
from the beegining
are you still getting the javascript error?
noo
can you show the code?
also, why don't you use ajax?
because it one of the projects goals to use php
uh ... what?
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
and how would that invalidate what i asked?
he said
dont use libraries
or fetch
that's dumb as hell
yeaa
but yeah, that invalidates what i said
i dont even why these data are not being inserted because i receive them in the network
this was working so well till yesterday
the names of the columns in the database start with capital leters
okay i will change in my code
the naming
nope
add a
var_dump($_POST);
before the if
okay
then?
then run it
if you don't see anything different, then you are missing something
still the same
are you sure that it is submitting to that file?
http://localhost:8080/ExpensesTracker/dashbord/dashbord.php
this is the path
i cleared cache
that's in your pc, i can't see it
i know
but i think its right
is this on github?
we worked on develop branch
https://github.com/anessheremeti/Expenses-Tracker/blob/develop/dashbord/dashbord.php <-- that file doesn't have any php code in it
i think
we did not push it yet
never mind mate
leave it
i will try to fix it
thanks for the effort
alright
it's very hard to help without the actual code and seeing it
but you're welcome
Can u dm me?
@ἔρως
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
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
yeah, sending credentials to a random person because that person offered to help is the easiest way to get your stuff hacked and ruined
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
are you getting javascript errors still? Do you see a POST to dashboard.php?
can you share your updated code?
when i do dev tools with 4g i see the request and data
but that data is not being inserted into database
you removed the hidden field
yes
but i dont think that
is the issue
That doesn't solve this problem
I'm telling you it is
okay
so i should
it is
add again
that field
yes, but with a different name
something like "sent"
<input type="hidden" name="sent" />
like this
This would be my recommendation
by the way, you NEED to do server-side validation too
primarily because there's other forms, and it looks like you'll want to be able to distinguish which one is being used
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
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
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
you can
but it's best to separate things, but you can
thanks again guys
peace
by the way, for dates, you MUST DO SERVER-SIDE VALIDATION
okay
for everything you must do server-side validation
the format of the date won't match the format of the input
what data type
should i use
varchar?
client-side validation is for user convenience only, serverside validation is for security
DATE
varchar is for small strings, like usernames and emails
and HASHED passwords
<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,
$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,
$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
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
please use code blocks, this is borderline unreadable without formatting
#how-to-ask-good-questions has some instructions on how to use them
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 arrayYea but look thats a seperated form
That i send
In message.txt
And thats why i changed the naming
And added another if
same concept applies
So i can keave
The same naming
yes it did
thanks so much
you're welcome