simpson_121919 Posted March 15, 2011 Share Posted March 15, 2011 I have been trying to insert data into my database thru php script with no luck. I think its has to do with not being able access my database. My question is when I use MySQL command client and enter the password and get mysql> does that mean I can use that password to access my databases as long as I point to the correct database with the mysqli_connect function. I dont think I fully understand how this works. I have everything stored on my local computer and I use "localhost" in mysqli_connect function. Quote Link to comment https://forums.phpfreaks.com/topic/230735-rookie-alert-inserting-data-with-php-script-question/ Share on other sites More sharing options...
cunoodle2 Posted March 15, 2011 Share Posted March 15, 2011 Honestly the best thing to start with is a little code. What do you have so far? Here is a very very rough tutorial on how to do it.. <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')"); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire', '33')"); mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/230735-rookie-alert-inserting-data-with-php-script-question/#findComment-1187890 Share on other sites More sharing options...
simpson_121919 Posted March 15, 2011 Author Share Posted March 15, 2011 Here is the code I have been using - <?php $first_name = $_POST['firstname']; $last_name = $_POST['lastname']; $when_it_happened = $_POST['whenithappened']; $how_long = $_POST['howlong']; $how_many = $_POST['howmany']; $alien_description = $_POST['aliendescription']; $what_they_did = $_POST['whattheydid']; $fang_spotted = $_POST['fangspotted']; $email = $_POST['email']; $other = $_POST['other']; $dbc = mysqli_connect('localhost', 'root', 'x', 'aliendatabase') or die('Error connecting to MySQL server.'); $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " . "how_many, alien_description, what_they_did, fang_spotted, other, email) " . "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " . "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')"; $result = mysqli_query($dbc, $query) or die('Error querying database.'); mysqli_close($dbc); echo 'Thanks for submitting the form.<br />'; echo 'You were abducted ' . $when_it_happened; echo ' and were gone for ' . $how_long . '<br />'; echo 'Number of aliens: ' . $how_many . '<br />'; echo 'Describe them: ' . $alien_description . '<br />'; echo 'The aliens did this: ' . $what_they_did . '<br />'; echo 'Was Fang there? ' . $fang_spotted . '<br />'; echo 'Other comments: ' . $other . '<br />'; echo 'Your email address is ' . $email; ?> I keep getting the die function read out 'Error querying database' I'm wondering if Im using the right username. How do I check to make sure I have the right username. Quote Link to comment https://forums.phpfreaks.com/topic/230735-rookie-alert-inserting-data-with-php-script-question/#findComment-1187900 Share on other sites More sharing options...
jcbones Posted March 16, 2011 Share Posted March 16, 2011 Try this: It doesn't fix anything, it only lets you know what the database thinks. <?php $first_name = $_POST['firstname']; $last_name = $_POST['lastname']; $when_it_happened = $_POST['whenithappened']; $how_long = $_POST['howlong']; $how_many = $_POST['howmany']; $alien_description = $_POST['aliendescription']; $what_they_did = $_POST['whattheydid']; $fang_spotted = $_POST['fangspotted']; $email = $_POST['email']; $other = $_POST['other']; $dbc = mysqli_connect('localhost', 'root', 'x', 'aliendatabase') or die('Error connecting to MySQL server: <br />' . mysqli_connect_error()); $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " . "how_many, alien_description, what_they_did, fang_spotted, other, email) " . "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " . "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')"; $result = mysqli_query($dbc, $query) or die('Error querying database: <br />' . mysqli_error($dbc)); mysqli_close($dbc); echo 'Thanks for submitting the form.<br />'; echo 'You were abducted ' . $when_it_happened; echo ' and were gone for ' . $how_long . '<br />'; echo 'Number of aliens: ' . $how_many . '<br />'; echo 'Describe them: ' . $alien_description . '<br />'; echo 'The aliens did this: ' . $what_they_did . '<br />'; echo 'Was Fang there? ' . $fang_spotted . '<br />'; echo 'Other comments: ' . $other . '<br />'; echo 'Your email address is ' . $email; ?> Quote Link to comment https://forums.phpfreaks.com/topic/230735-rookie-alert-inserting-data-with-php-script-question/#findComment-1187989 Share on other sites More sharing options...
simpson_121919 Posted March 16, 2011 Author Share Posted March 16, 2011 Thanks that did the job. I had spelled my database table name wrong. I think Im going to realy like those functions. Thanks again I really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/230735-rookie-alert-inserting-data-with-php-script-question/#findComment-1187994 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.