beaudoin Posted August 11, 2009 Share Posted August 11, 2009 Hey All, I'm very new to PHP, and am a young guy at that (16), no don't get me wrong I'm a smart kid, I just need time to perfect my skills Anyhow, the following the the script i wrote. Here's basically what I'm trying to do: 1) A user, in a previous HTML page has inputted data into a form 2) They press submit and the data SHOULD be processed through a PHP form 3) I want that data to be entered into a database table. The database and table are fully set up. 4) The script will have created the user and the user will now be able to login. www.mobitek.ca/createuser-builder/formto.html is the form, and there the insert fails, as per the error that was echoed. Here's the script: <?php $username = $_POST["username"]; $password = $_POST["password"]; $name = $_POST["name"]; $address1 = $_POST["address1"]; $address2 = $_POST["address2"]; $city = $_POST["city"]; $state = $_POST["state"]; $zip = $_POST["zip"]; $country1 = $_POST["country1"]; $phone = $_POST["phone"]; $email = $_POST["email"]; $admin = $_POST["admin"]; $country2 = $_POST["country2"]; $link = mysql_connect('localhost', 'mobitek_build', 'l9hJpA5PfU4H'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; $query = "INSERT INTO `compb_users` ( `id` , `username` , `password` , `name` , `address` , `city` , `state` , `zip` , `country1` , `phone` , `email` , `admin` , `country2` ) VALUES ( '$id', '$username', '$password', '$name', '$address', '$city', '$state', '$zip', '$country1', '$phone', '$email', '$admin', '$country2',);"; mysql_query($query) or die ('Error creating user'); echo ("Thank you"); // I eventually want this said after successful creation // echo "Thank you - You may now sign in with your username:" ".$username "; ?> Anyone able to help solve what I'm sure is a very simple task? I hope to learn Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/ Share on other sites More sharing options...
guyfromfl Posted August 11, 2009 Share Posted August 11, 2009 where is it going wrong?? what method are you using in your form to send the data to this script? Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-895981 Share on other sites More sharing options...
guyfromfl Posted August 11, 2009 Share Posted August 11, 2009 take the ; out of the sql query and , after $country2 Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-895983 Share on other sites More sharing options...
beaudoin Posted August 11, 2009 Author Share Posted August 11, 2009 mysql_query($query) or die ('Error creating user'); SHOULD be mysql_query($query) or die ('Error creating user') -- is the the line your referring to or is it just at the end of where I define $query? Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-895985 Share on other sites More sharing options...
guyfromfl Posted August 11, 2009 Share Posted August 11, 2009 change this line: $query = "INSERT INTO `compb_users` ( `id` , `username` , `password` , `name` , `address` , `city` , `state` , `zip` , `country1` , `phone` , `email` , `admin` , `country2` ) VALUES ( '$id', '$username', '$password', '$name', '$address', '$city', '$state', '$zip', '$country1', '$phone', '$email', '$admin', '$country2')"; Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-895986 Share on other sites More sharing options...
beaudoin Posted August 11, 2009 Author Share Posted August 11, 2009 Ok, I've changed all that and now I've got this error: Parse error: syntax error, unexpected T_ECHO in /home/mobitek/public_html/createuser-builder/create.php on line 29 What does this mean? Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-895989 Share on other sites More sharing options...
guyfromfl Posted August 11, 2009 Share Posted August 11, 2009 try taking the () off around "Thank You" echo "Thank you"; Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-895991 Share on other sites More sharing options...
beaudoin Posted August 11, 2009 Author Share Posted August 11, 2009 try taking the () off around "Thank You" echo "Thank you"; Yeah, I was looking at that, and it seems to me that when I took them away, the error still persisted... I will try clearing my cache just in case. BTW thanks a tone for your help so far! Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-895996 Share on other sites More sharing options...
guyfromfl Posted August 11, 2009 Share Posted August 11, 2009 yea man...let me know what you get Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-895998 Share on other sites More sharing options...
guyfromfl Posted August 11, 2009 Share Posted August 11, 2009 also try this: $query = "INSERT INTO compb_users ( id , username , password , name, address , city , state , zip , country1 , phone , email , admin , country2 ) VALUES ( '$id', '$username', '$password', '$name', '$address', '$city', '$state', '$zip', '$country1', '$phone', '$email', '$admin', '$country2')"; Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-895999 Share on other sites More sharing options...
beaudoin Posted August 11, 2009 Author Share Posted August 11, 2009 Yeah, It didn't seem to do anything when changing from ` to ' and refreshing the cache didn't solve my idea either. Anything else you could think of? -- I'm looking around on php.net as we speak.... Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-896003 Share on other sites More sharing options...
guyfromfl Posted August 11, 2009 Share Posted August 11, 2009 what I meant was in the $query definition take out the ' around the database fields and table names. comment this out of your original code and paste this version $query = "INSERT INTO compb_users ( id , username , password , name, address , city , state , zip , country1 , phone , email , admin , country2 ) VALUES ( '$id', '$username', '$password', '$name', '$address', '$city', '$state', '$zip', '$country1', '$phone', '$email', '$admin', '$country2')"; One more thing, don't post your server credintials on open forums (like your sql username and password)... you dunno who is reading it. Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-896006 Share on other sites More sharing options...
beaudoin Posted August 11, 2009 Author Share Posted August 11, 2009 Ok, I have replaced the code exactly the way you have written it here. Also, this is just a test database, so I couldn't care less about the password out in the open... but Thank you for noticing! And editing that piece of code didn't help either :'( Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-896025 Share on other sites More sharing options...
guyfromfl Posted August 12, 2009 Share Posted August 12, 2009 Another problem, but I don't think the compiler has gotten that far is you don't pass any database info. you need a connector object for mysql $conn = mysql_select_db(<<database name>>); and don't you want this instead for your if statement if (!$link) { die('Could not connect: ' . mysql_error()); } else { echo 'Connected successfully'; } Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-896127 Share on other sites More sharing options...
guyfromfl Posted August 12, 2009 Share Posted August 12, 2009 im not 100% on this but i think you need to pass the query to an object...I always do so I don't know if you have to or not. try $result = mysql_query($query) or die ('Error creating user'); Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-896137 Share on other sites More sharing options...
beaudoin Posted August 12, 2009 Author Share Posted August 12, 2009 I solved the t_echo problem... I also added $result into that line. Now, I'm not sure what you mean about connecting to the database without the if statement I used. If so, where do you put the password, if I use your example.... I look up that now. So far it seems to connect to the database, but fails to create the user every time. Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-896144 Share on other sites More sharing options...
beaudoin Posted August 12, 2009 Author Share Posted August 12, 2009 Here's the point I'm at now with the code... I think I'm getting there... <?php $username = $_POST["username"]; $password = $_POST["password"]; $name = $_POST["name"]; $address1 = $_POST["address1"]; $address2 = $_POST["address2"]; $city = $_POST["city"]; $state = $_POST["state"]; $zip = $_POST["zip"]; $country1 = $_POST["country1"]; $phone = $_POST["phone"]; $email = $_POST["email"]; $admin = $_POST["admin"]; $country2 = $_POST["country2"]; // CONNECTION TO DATABASE $username = "mobitek_build"; $password = "l9hJpA5PfU4H"; $hostname = "localhost"; //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "Connected to MySQL<br>"; //select a database to work with $selected = mysql_select_db("mobitek_build",$dbhandle) or die("Could not select builders database"); // INSERTING INTO DATABASE $query = "INSERT INTO compb_users ( id , username , password , name, address , city , state , zip , country1 , phone , email , admin , country2 ) VALUES ( '$id', '$username', '$password', '$name', '$address', '$city', '$state', '$zip', '$country1', '$phone', '$email', '$admin', '$country2')"; $result = mysql_query($query) or die ('Error creating user') // echo "Thank you"; ?> I've commented some stuff out for now, as you might see. Still the problem is now, trying to get the database to accept what being written to it, rather than creating the failure error. Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-896149 Share on other sites More sharing options...
co.ador Posted August 12, 2009 Share Posted August 12, 2009 Can you show us the form? Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-896153 Share on other sites More sharing options...
beaudoin Posted August 12, 2009 Author Share Posted August 12, 2009 Why, of course! <table border="0"> <form name="create-user" method="post" action="create.php"> <tr> <td><p><b> Preferred Username:</b></td> <td><input type="text" value="" name="username"></td> </tr> <tr> <td><p><b> Preferred Password:</b></td> <td><input type="text" value="" name="password"></td> </tr> <tr> <td><p><b>Name:</b></td> <td><input type="text" value="" name="name"> </td> </tr> <tr> <td><p><b>Address:</b></td> <td><input type="text" value="" name="address1"> </td> </tr> <tr> <td><p><b>Address (line 2):</b></td> <td><input type="text" value="" name="address2"> </td> </tr> <tr> <td><p><b>City:</b></td> <td><input type="text" value="" name="city"> </td> </tr> <tr> <td><p><b>Province/State:</b></td> <td><input type="text" value="" name="state"> </td> </tr> <tr> <td><p><b>Postal Code/ZIP:</b></td> <td><input type="text" value="" name="zip"> </td> </tr> <tr> <td><p><b>Country:</b></td> <td><input type="text" value="" name="country1"> </td> </tr> <tr> <td><p><b>Phone:</b></td> <td><input type="text" value="" name="phone"> </td> </tr> <tr> <td><p><b>Email:</b></td> <td><input type="text" value="" name="email"> </td> </tr> </table> <!-- hidden values --> <input type="hidden" value="0" name="admin"> <input type="hidden" value="" name="country2"> <input type="submit" name="mysubmit" value="Register!"> <input type="submit" name="myreset" value="Erase Data"> </form> there is a couple hidden values that are not needed when submitted anyways, should they even be included? Anyways, let me know what you think needs changes. Thanks again, Justin Link to comment https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/#findComment-896159 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.