Nishchay Posted March 3, 2013 Share Posted March 3, 2013 Hey guys i'am basically making a database driven website and i need to provide my customers a interactive form on the website . When they enter their details their info should enter my database.for that purpose i firstly have connected my database to my webpage.now i want this :user should type firstname and lastname in a form and that should enter my fname and lname attribute in my table Test in the database. i tried doing this but it didnt work outI've made a html file ( k.html) whose code is below :<html><body><form action="insert.php" method="post">Firstname: <input type="text" name="firstname">Lastname: <input type="text" name="lastname"><input type="submit"></form></body></html>Now i've made an insert.php file whose code is below :<?php$c=mysql_connect("localhost","username","password") // (My personal)if(!$c){echo("Unable to connect to the database at this time ");}else{echo("Connection to the database was succesfull :)");}?> <?phpmysql_select_db("a6279515_nis",$c);if(!mysql_select_db("a6279515_nis",$c)){echo("Can't establish database");}?> <?php$sql="INSERT INTO Test (fname,lname)VALUES('$_POST[firstname]','$_POST[lastname]');if (!mysqli_query($c,$sql)){die('Error: ' . mysqli_error());}echo "1 record added";mysqli_close($c);?>The error which my page shows is that : Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in /home/a6279515/public_html/insert.php on line 21Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/a6279515/public_html/insert.php on line 23 Quote Link to comment https://forums.phpfreaks.com/topic/275151-need-help-with-mysql-query-in-php-urgent/ Share on other sites More sharing options...
Christian F. Posted March 3, 2013 Share Posted March 3, 2013 Please read the following post on how to debug your SQL errors. Also, you should be using either MySQLI(mproved) or PDO. Seeing as the old mysql library is no longer maintained and deprecated in 5.5. PS: Labelling a post "urgent" will not get you faster help. If anything, it might cause people to skip it. If you need urgent help, then the best bet is to pay someone for it. On a forum all threads have the same priority, as it is a purely voluntary effort from those who help. Quote Link to comment https://forums.phpfreaks.com/topic/275151-need-help-with-mysql-query-in-php-urgent/#findComment-1416142 Share on other sites More sharing options...
redarrow Posted March 3, 2013 Share Posted March 3, 2013 (edited) <?php $con=mysql_connect("localhost","database_name","database_password")or die("mysql_errow()"); $db=mysql_select_db("database_name",$con)or die("mysql_errow()"); if($_POST['submit']){ $firstname=mysql_real_escape_string($_POST['firstname']); $lastname=mysql_real_escape_string($_POST['lastname']); if( empty($firstname) || empty($lastname)){ echo" Sorry you need to enter a firstname and lastname"; die(); } $insert="INSERT INTO database_field(firstname,lastname)VALUES('$fistname','$lastname'); $insert_query=mysql_query($insert)or die("database error"); if($insert){ echo" The database has your $firstname and $lastname cheers"; )else{ echo" sorry there a problam some where email me @ what ever.com"; } ?> <html> <head> <title>submit username and lastname</title> </head> <body> <table> <tr> <td> <form method="POST" action=""> <br /> <br /> Please add your name. <input type="text" name="firstname"> <br /> <br /> Please add your surname. <input type="text" name="lastname"> <br /> <br /> <input type="submit" name="submit"> </td> </tr> </table> <br /> </body> </html> Not tested at all. Edited March 3, 2013 by redarrow Quote Link to comment https://forums.phpfreaks.com/topic/275151-need-help-with-mysql-query-in-php-urgent/#findComment-1416150 Share on other sites More sharing options...
redarrow Posted March 3, 2013 Share Posted March 3, 2013 (edited) <?php $con=mysql_connect("localhost","username","password")or die("mysql_errow()"); $db=mysql_select_db("database_name",$con)or die("mysql_errow()"); if($_POST['submit']){ $firstname=mysql_real_escape_string($_POST['firstname']); $lastname=mysql_real_escape_string($_POST['lastname']); if( empty($firstname) || empty($lastname)){ echo" Sorry you need to enter a firstname and lastname <a href='".$_SERVER['PHP_SELF']."'>TRY AGIN</a>"; die(); } $insert="INSERT INTO database_field(firstname,lastname)VALUES('$firstname','$lastname')"; $insert_query=mysql_query($insert)or die('database error()'); if($insert_query){ echo" The database has your $firstname and $lastname cheers"; }else{ echo" sorry there a problam some where email me @ what ever.com"; } } ?> <html> <head> <title>submit username and lastname</title> </head> <body> <table> <tr> <td> <form method="POST" action=""> <br /> <br /> Please add your name. <br /> <input type="text" name="firstname"> <br /> <br /> Please add your surname. <br /> <input type="text" name="lastname"> <br /> <br /> <input type="submit" name="submit"> </td> </tr> </table> <br /> </body> </html> version 2 complete ... 10/10 i must say my self. Edited March 3, 2013 by redarrow Quote Link to comment https://forums.phpfreaks.com/topic/275151-need-help-with-mysql-query-in-php-urgent/#findComment-1416177 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.