loudog Posted May 15, 2011 Share Posted May 15, 2011 hey guys i've been copying alot of free php scripts in the web but this last one i keep getting the error message. it won't let me connect . here are the scripts. (page1) insert.php <html> <head> <title>Untitled Document</title> </head> <body> <form name="form1" action=" insert_ac.php" method="post"> <table width="300" border="0" align="center" cellpadding="0" cellspacing="1"> <tr> <td> <table width="100%" border="0 cellspacing="1" cellpadding="3"> <tr> <td colspan="3"><strong>Insert Data Into mySQL Database</strong></td> </tr> <tr> <td width="71">Name</td> <td width=""6>:</td> <td width="301"> <input name="name" type="text" id="name"> </td> </tr> <tr> <td>Lastname</td> <td>:</td> <td> <input name="lastname" type="text" id="lastname"></td> </tr> <tr> <td>Email</td> <td>:</td> <td><input name="email" type="text" id="email"></td> </tr> <tr> <td colspan="3" align="center"><input type ="submit" name="Submit" value="Submit"></td> </tr> </table> </form> </body> </html> (page2) insert_ac.php <?php //Connecting to server and selecting databaase. mysql_connect("localhost", "root", "")or die("cannot connect"); mysql_select_db("test_create_db") or die("cannot select DB"); //Get values from form. $name=$_POST['name']; $lastname=$_POST['lastname']; $email=$_POST['email']; //Insert data into mysql $sql="INSERT INTO web_members(name,lastname,email)VALUES($name,$lastname,$email)"; $result=mysql_query($sql); //if succesfully insert data into database,display message "Successful". if($result){ echo "Successful"; echo "<BR>"; echo "<a href='insert.php'>Back to main page</a>"; } else{ echo "ERROR"; } //close connection mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/236486-help-plz/ Share on other sites More sharing options...
jcbones Posted May 15, 2011 Share Posted May 15, 2011 So, do you have a database set up? What is the error you are getting? Quote Link to comment https://forums.phpfreaks.com/topic/236486-help-plz/#findComment-1215777 Share on other sites More sharing options...
loudog Posted May 15, 2011 Author Share Posted May 15, 2011 yes i have the database set up and the error i keep getting its the one thar i have made bold. ($result){ echo "Successful"; echo "<BR>"; echo "<a href='insert.php'>Back to main page</a>"; } else{ echo "ERROR";<- } //close connection mysql_close(); here is my data base config <?php $host=mysql_connect("localhost", "root", "")or die("cannot connect to server"); mysql_select_db("test_create_db")or die("connot select db"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/236486-help-plz/#findComment-1215786 Share on other sites More sharing options...
MidOhioIT Posted May 15, 2011 Share Posted May 15, 2011 try this instead: change your connection string to: $db=mysql_connect("localhost", "root", "pass...")or die("cannot connect"); You sure you have root access or is that what you named your username? that is a bad idea if you named your username that... then change your query to: $sql=mysql_query("INSERT INTO web_members(name,lastname,email) VALUES('$name','$lastname','$email' ",$db); * put a space before "VALUES" if you still get an error, its either your connection itself or the query not having values, or not agreeing with your table web_members. if your still having issues try doing this after your insert: echo "insert statement: INSERT INTO web_members(name,lastname,email) VALUES('$name','$lastname','$email' <br>"; at least you can see your insert and then run it directly on the db to see if you get a syntax issue. you could also always do or die commands afterwards and that would tell you if your sql syntax is wrong. Quote Link to comment https://forums.phpfreaks.com/topic/236486-help-plz/#findComment-1215828 Share on other sites More sharing options...
loudog Posted May 16, 2011 Author Share Posted May 16, 2011 thanks guys for all your help i found the mistake . I didn't put the single colons before, in between, and after the variables on the values and i forgot the space as well. //Insert data into mysql the error was on this line->$sql="INSERT INTO web_members(name,lastname,email)VALUES($name,$lastname,$email)"; the correct way was like this ->$sql="INSERT INTO web_members(name, lastname, email) VALUES ('$name','$lastname','$email')"; $result=mysql_query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/236486-help-plz/#findComment-1215844 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.