I want to make it possible for the users to insert data into a database using a form, so that other users can see that data when they login. How can I do this using PHP?
Create a sample script, in this script place the following and the following only…
<?php echo date('r'); ?>
What do you get displayed when you browse to it?
The next line under where you set the error reporting level add this line:
ini_set('display_errors', 1);
Once saved, try the script again, you should then get errors displayed (you’ve probably got the display of errors turned off atm).
Is your form sending anything valid to your form handling script?
Do this after the error_reporting() call you were shown earlier.
var_dump( $_POST );
Take a look at the contents, is that what you submitted?
Also, you should be quoting the keys of your post array like this:
$_POST[‘field3’]
PHP is going to be chucking out loads of error notices otherwise.
The passing of $conn is mandatory when using mysqli procedurally, if it doesn;t get $conn it will throw an error:
Warning: mysqli_query() expects at least 2 parameters, 1 given…
Add:
error_reporting(E_ALL);
to the top of the script.
Review the man page for correct arguments:
http://us2.php.net/manual/en/mysqli.query.php
Hint: Your $conn is in the wrong spot. You don’t really need to pass it anyways.
The fact that nothing is being echoed seems to indicate the script is never actually being called. Add:
die($sql);
Right after you $sql = statement.
That will verify the script is running and verify your sql statement.
Connect the page to a database.
What type of database you have available will determine the advice from that point on, but most find that [fphp]mysqli[/fphp] is the available type.
It not the sort of thing that can be answered in a quick post. Plenty of php/mysql tutorials out there. Here is one:
Still nothing. What does this mean?
It means your server configuration is buggered.
Does it serve .html files OK? What platform are you using? What configuration have you done?
Uhm, is it normal that I still get a blank page?
Yep. My mistake in reading the manual. Hopefully the error_reporting statement will give some hints.
I followed the example on the w3schools website, but the code doesn’t work:
<?php
$conn = mysqli_connect('localhost','root','root','database');
if (!$conn)
{
echo 'Could not connect: ' . mysqli_connect_error();
exit;
}
$sql="INSERT INTO database_table (field1, field2, field3, field4, field5, field6)
VALUES
('$_POST[field1]','$_POST[field2]','$_POST[field3]','$_POST[field4]','$_POST[field5]','$_POST[field6]')";
if (!mysqli_query($sql,$conn))
{
echo 'Error: ' . mysqli_connect_error());
}
echo "1 record added";
mysqli_close($conn);
?>
When I insert the data into the form I get a blank page and no messages. What am I doing wrong?
Thank you very much, there’s a section in there about how to insert info to a database from a form
Unfortunately nothing happened, still blank
Sorry for my late answer.
I’ve tried what you suggested but I still get a blank page when I click on Submit, nothing happens.
Thanks. How can I do that practically? Meaning, how should I write a form that inserts its content to a db?
What do you mean?
What platform are you using? What configuration have you done?
I’m using MAMP and I haven’t done any configuration whatsoever. It’s strange because the login system that I’ve build works flawlessly…