Jump to content

Help with inserting data into database


beaudoin

Recommended Posts

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?  :facewall: I hope to learn ;) 

Link to comment
https://forums.phpfreaks.com/topic/169831-help-with-inserting-data-into-database/
Share on other sites

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')";

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!

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')";

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.

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 :'(

 

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';
}

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.  :facewall:

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.