Jump to content

Please help, trying to input data into DB driving me crazy.


VinceGledhill

Recommended Posts

Hi People.

 

I am trying to insert data from a form into my database.

 

Now I have the following code to connect to the DB to update a table so I know that I can connect to the DB ok

 

<?php 
// this code I got from the new boston, PHP tutorial 25 in selecting a mysql db
// opens connection to mysql server
$dbc = mysql_connect('localhost', 'VinnyG', 'thepassword');
if (!$dbc) {
die("Not Connected:" . mysql_error ());
}
// select database
$db_selected = mysql_select_db ("sitename",$dbc);
if(!$db_selected)
{
die("can not connect:" . mysql_error ());

}
// testing code

$query="UPDATE users SET username = 'testing testing' WHERE user_id = '2'";
$result=mysql_query($query);


?>

 

Now here is the code from my form.

 

</head>

<body>
<?php
//include "connection_file.php" 
//include "config01.php"

$username = "username";
$height_above = "height_above";
$mb_diff = "mb_diff";
$alternative = "alternative"; 

?>

<form name = 'form1' method = 'post' action='config01.php'>
<table width="700" border="1" cellspacing="5" cellpadding="5">
  <caption>
    Submit Your Airfield Details
  </caption>
  <tr>
    <td width="100"> </td>
    <td width="200">Your Name</td>
    <td width="200"><input type='text' name='username' maxlength='30'></td>
    <td width="100"> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Height Above MSL</td>
    <td><input type='text' name='height_above'maxlength= '30'></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Mb Difference</td>
    <td><input type='text' name='mb_diff'maxlength='40'></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Alternative Airfield</td>
    <td><input type='text' name='alternative' maxlength='30'></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td><input type='submit' name='submit' value='post' /></td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>
</form>
<?php
$sql01 = "INSERT INTO users SET user_id = '', username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative";
$result=mysql_query($sql01);
?>

</body>
</html>

 

here is the config01.php that the form refers to in the 'action' above.

 

<?php
$host = 'localhost';
$username = 'VinnyG';
$password = 'thepassword';
$db_name = 'sitename';

//connect to database
mysql_connect ("$host","$username","password")or die ("cannot connect to server");
mysql_select_db ("db_name") or die ("cannot select DB");

?>

 

Please could someone look at the above code and tell me where I'm going wrong.  I can connect to the DB and update using the top script but I can't submit the form for some reason.  I get a "cannot connect to server" message.

 

Please someone help.  It's been driving me crazy for the past two days.

 

Regards

VinceG

http://www.microlightforum.com

 

 

Link to comment
Share on other sites

in

//connect to database
mysql_connect ("$host","$username","password")or die ("cannot connect to server");
mysql_select_db ("db_name") or die ("cannot select DB");

 

 

you $username password is "password" or the content of the variable $password?... same question for "db_name"

Link to comment
Share on other sites

Hi Mate,

 

Here...

 

mysql_connect ("$host","$username","password")or die ("cannot connect to server");

 

You have missed out the $ from the password, that will be why you cant connect.

 

Also where you have your variables, You havent used the posted information.

 

Instead of....

$username = "username";

 

use...

$username = $_POST['username'];

 

Do that for all your form data.

 

Also I think you will have to move the query that you use to write to the database to the config01.php page.

 

Hope it helps

 

 

Link to comment
Share on other sites

Thanks for your input guys.

 

I have corrected the errors but still it doesn't move stuff into the database.  I thought that it could be the fact that the user_id which is the primary and is auto-incrementing was the problem so I took the info out.

 

Now all I get is everything passed to config01.php and a blank screen.  Please someone help before I loose the plot altogether.

<body>
<?php
//include "connection_file.php" 
//include "config01.php"
//$user_id = $_POST[""];
$username = $_POST["username"];
$height_above = $_POST["height_above"];
$mb_diff = $_POST["mb_diff"];
$alternative = $_POST["alternative"]; 

?>

<form name = 'form1' method = 'post' action='config01.php'>
<table width="700" border="1" cellspacing="5" cellpadding="5">
  <caption>
    Submit Your Airfield Details
  </caption>
  <tr>
    <td width="100"> </td>
    <td width="200">Your Name</td>
    <td width="200"><input type='text' name='username' maxlength='30'></td>
    <td width="100"> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Height Above MSL</td>
    <td><input type='text' name='height_above'maxlength= '30'></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Mb Difference</td>
    <td><input type='text' name='mb_diff'maxlength='40'></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Alternative Airfield</td>
    <td><input type='text' name='alternative' maxlength='30'></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td><input type='submit' name='submit' value='post' /></td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>
</form>


</body>
</html>

 

Here is the config01.php

 

I move the "post it" stuff out of the form and into this file but still nothing, no errors now though but still nothing in the DB.

<?php
$host = 'localhost';
$username = 'VinnyG';
$password = 'thepassword';
$db_name = 'sitename';

//connect to database
mysql_connect ("$host","$username","$password")or die ("cannot connect to server");
mysql_select_db ("$db_name") or die ("cannot select DB");


$sql01 = "INSERT INTO users SET username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative";
$result=mysql_query($sql01);


?>

 

If I could see what was going on it would help.  But a blank screen is no help whatsoever.

 

Regards

VinceG

 

 

Link to comment
Share on other sites

Instead of...

 

$sql01 = "INSERT INTO users SET username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative";
$result=mysql_query($sql01);

 

Use...

 

mysql_query("INSERT INTO users VALUES ('$username','$height_above','$mb_diff','$alternative')");

 

The user_id you mention wouldnt be the problem.

With the code i have given you above, you need to make sure you have the total number of form fields inserted. For example if you have 5 fields in your table, the insert should contain 5 values.

 

Try it and let us know.

 

Thanks

Link to comment
Share on other sites

Hi Ken.

I'm commenting out the includes because with them in the form won't load.

 

Perky416.

 

I actually have 5 fields in the db.  How do I handle the auto incrementing user_id (primary field)? (see picture attached)

 

Code now as below...

 

Still get the can't connect message

<body>
<?php
//include "connection_file.php" 
//include "config01.php"
//$user_id = $_POST[""];
//$username = $_POST["username"];
//$height_above = $_POST["height_above"];
//$mb_diff = $_POST["mb_diff"];
//$alternative = $_POST["alternative"]; 

?>

<form name = 'form1' method = 'post' action='config01.php'>
<table width="700" border="1" cellspacing="5" cellpadding="5">
  <caption>
    Submit Your Airfield Details
  </caption>
  <tr>
    <td width="100"> </td>
    <td width="200">Your Name</td>
    <td width="200"><input type='text' name='username' maxlength='30'></td>
    <td width="100"> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Height Above MSL</td>
    <td><input type='text' name='height_above'maxlength= '30'></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Mb Difference</td>
    <td><input type='text' name='mb_diff'maxlength='40'></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Alternative Airfield</td>
    <td><input type='text' name='alternative' maxlength='30'></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td><input type='submit' name='submit' value='post' /></td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>
</form>


</body>
</html>

 

Here is the code for the config01.php

<?php
$host = 'localhost';
$username = 'VinnyG';
$password = 'thepassword';
$db_name = 'sitename';

$username = $_POST["username"];
$height_above = $_POST["height_above"];
$mb_diff = $_POST["mb_diff"];
$alternative = $_POST["alternative"]; 

//connect to database
mysql_connect ("$host","$username","$password")or die ("cannot connect to server for fuck sake this is driving me potty");
mysql_select_db ("$db_name") or die ("cannot select DB");

/*
$sql01 = "INSERT INTO users SET username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative";
$result=mysql_query($sql01);
*/
mysql_query("INSERT INTO users VALUES ('$username','$height_above','$mb_diff','$alternative')");

?>

 

Thanks again for your valued input guys.  I've been to the local shop for "beer"

 

Regards

Vince

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

ok, the problem with the connection is that you are over writing the contents of $username before you run the connection string.  so you now have the form input user name rather than the database connection user name.  rather than just having

or die("cannot connect to server for fuck sake this is driving me potty");

use

or die ('Error During Connect:<br>'.mysql_error());

This will let you see what error the database is returning.

 

Now, change

 $username = "VinnyG";

to

$usr = "VinnyG";

and then update your mysql_connect to be

mysql_connect ("$host","$usr","$password") or die ('Error During Connect:<br>'.mysql_error());

 

that should get your connection sorted out.  you should change all your

or die()

statements that are attached to the use of mysql functions to report the mysql_error() as well as hold some statement relevent to what the php was trying to do when the error occured.

 

To handle the INSERT INTO you just name the fields that you want to the values inserted into in the order that you are presenting the values to the table.

mysql_query("INSERT INTO users (username, height, difference, alternative) VALUES ('bob', 10, 5, 'something different')"

Notice that any values that are being insert that are not integers are not wraped in single quotes and that there are spaces after every comma.

Link to comment
Share on other sites

Thanks for your input Muddy Funster.

 

I have updated as you said and I now have got rid of the errors and show the form.  However, when I fill it in and click the button I get the config01.php page (which is now blank) and no error messages and nothing into the DB.

 

Here's my two files

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php include "config01.php"?>

<form name = 'form1' method = 'post' action='config01.php'>
<table width="700" border="1" cellspacing="5" cellpadding="5">
  <caption>
    Submit Your Airfield Details
  </caption>
  <tr>
    <td width="100"> </td>
    <td width="200">Your Name</td>
    <td width="200"><input type='text' name='username' maxlength='30'></td>
    <td width="100"> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Height Above MSL</td>
    <td><input type='text' name='height_above'maxlength= '30'></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Mb Difference</td>
    <td><input type='text' name='mb_diff'maxlength='40'></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td>Alternative Airfield</td>
    <td><input type='text' name='alternative' maxlength='30'></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td><input type='submit' name='submit' value='post' /></td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>
</form>


</body>
</html>

 

 

<?php
$host = 'localhost';
$usr = "VinnyG";
$password = 'thepassword';
$db_name = 'sitename';

$username = $_POST["username"];
$height_above = $_POST["height_above"];
$mb_diff = $_POST["mb_diff"];
$alternative = $_POST["alternative"]; 

//connect to database
mysql_connect ("$host","$usr","$password") or die ('Error During Connect:<br>'.mysql_error());
mysql_select_db ("$db_name") or die ("cannot select DB");

/*
$sql01 = "INSERT INTO users SET username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative";
$result=mysql_query($sql01);
*/
mysql_query("INSERT INTO users VALUES ('$username','$height_above','$mb_diff','$alternative')");

?>

 

Link to comment
Share on other sites

I've changed the die statements.

 

and I've changed the insert and the die.  I'm still getting blank screen and nothing inserted.  Sorry for being THICK. 

 

I really am a newbie and it's driving me potty.

 

<?php
$host = 'localhost';
$usr = "VinnyG";
$password = 'thepassword';
$db_name = 'sitename';

$username = $_POST["username"];
$height_above = $_POST["height_above"];
$mb_diff = $_POST["mb_diff"];
$alternative = $_POST["alternative"]; 

//connect to database
mysql_connect ("$host","$usr","$password") or die ('Error During Connect:<br>'.mysql_error());
mysql_select_db ("$db_name") or die ('Error Selecting DB:<br>'.mysql_error());

/*
$sql01 = "INSERT INTO users SET username = '$username',height_above = '$height_above', mb_diff = $mb_diff, alternative = $alternative";
$result=mysql_query($sql01);
*/
//mysql_query("INSERT INTO users VALUES ('$username','$height_above','$mb_diff','$alternative')");
mysql_query("INSERT INTO users (username, height_above, mb_diff, alternative) VALUES ('$username', '$height_above', '$mb_diff', '$alternative'"); 

?>

 

Regards

VinceG

Link to comment
Share on other sites

ok - try this:

$insert_query = "INSERT INTO users (username, height_above, mb_diff, alternative) VALUES ('$username', '$height_above', '$mb_diff', '$alternative')";
$insert_action = mysql_query($insert_query) or die ('Error Dring Insert :<br>'.mysql_error().'<br><br>Error occured running the following code :<br>'.$insert_query);

 

that is replace your current

mysql_query("INSERT INTO users (username, height_above, mb_diff, alternative) VALUES ('$username', '$height_above', '$mb_diff', '$alternative'"); 

which is missing the closing bracket from the VALUES statement in the SQL...

Link to comment
Share on other sites

Change the last part of that code to the below and see what it gives you.

 

//mysql_query("INSERT INTO users VALUES ('$username','$height_above','$mb_diff','$alternative')"); CHANGES FOLLOW THIS LINE . . 
$query = "INSERT INTO users (username, height_above, mb_diff, alternative) VALUES ('$username', '$height_above', '$mb_diff', '$alternative'";
if( !$result = mysql_query($query) ) {
     echo "<br>Query: $query<br>Produced error: " . mysql_error() .'<br>';
} else {
     echo "Query ran and inserted " . mysql_affected_rows() . "row(s).";
}
?>

Link to comment
Share on other sites

Muddy Funster.  Thanks for your patience with this Thicko.

 

I've done as you said and now it is "sort of" working.  I am getting TWO rows inserted into the DB.  One is a new user ID and everything else is empty.  Then another row with what I put into the form???

 

Pikachu2000, Brilliant, thanks for your input, it's now Working as it should be.

 

Result people thanks a bunch. 

 

I'll be back with more I'm sure.  Hope one day to put something back.  Like I am doing to my users at www.microlightforum.com and www.microlight.co

 

On behalf of all my forum members, thanks guys.  You've been brilliant with this THICKO.

 

Regards

VinceG

Link to comment
Share on other sites

One thing I haven't got though is any feedback. 

 

I see from your code there is supposed to be some feedback

echo "Query ran and inserted " . mysql_affected_rows() . "row(s).";

 

I just got a blank page?????? Any ideas why?  Because when I've finished this I really would like some feedback to the users.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.