Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERTING INTO MYSQL

Status
Not open for further replies.

dadms

Technical User
Mar 8, 2003
67
0
0
US
PLEASE HELP, I CANT SEEM TO GET MY PHP SCRIPT WORKING. I DONT GET A CONNECTION ERROR. I GET THIS....

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /usr/home/panao3/public_html/formuser.php on line 11
there is a trouble2

PHP SCRIPT
<?php
//open connection

$dbh=mysql_connect(&quot;localhost&quot;, &quot;panao3_matt&quot;, &quot;Kiani123&quot;) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db(&quot;panao3_tierii&quot;);

//create SQL statement
$sql = &quot;INSERT INTO user (id, name) VALUES ('', '$_POST[name]')&quot;;

// execute the SQL statement
if (mysql_query($sql, $conn)) {
echo &quot;record added!&quot;;
} else {
echo &quot;there is a trouble2&quot;;
}
?>

HTML FILE
HTML>
<HEAD>
<TITLE>test</TITLE>
</HEAD>
<BODY>

<FORM METHOD=&quot;POST&quot; ACTION=&quot;formuser.php&quot; >
<!----name---->
<table width=&quot;100%&quot; border=&quot;0&quot; align=&quot;center&quot;>
<tr>
<td colspan=&quot;2&quot; style=&quot;border-bottom-style: solid; border-bottom-width: 1&quot; width=&quot;941&quot;>
<p align=&quot;center&quot;>DATABASE SUBMITTAL FORM</td>
</tr>

<tr>
<td width=&quot;939&quot; colspan=&quot;2&quot; style=&quot;border-style: solid; border-width: 1&quot;>&nbsp;</td>
</tr>

<tr>
<td width=&quot;1&quot; style=&quot;border-style: none; border-width: medium&quot;><b>AUTHOR:</b></td><!--spacer--->
<td width=&quot;1043&quot; style=&quot;border-style: none; border-width: medium&quot;>
&nbsp;<input type=&quot;text&quot; name=&quot;name&quot; size=&quot;20&quot;></td>
</tr>
</table>
<p align=&quot;center&quot;>
<INPUT TYPE=SUBMIT VALUE=&quot;Submit Form&quot;>
<INPUT TYPE=RESET VALUE=&quot;Reset Form&quot;> </p>
</FORM>
<HR>
</BODY>
</HTML>

MYSQL TABLE user
id int(15) UNSIGNED No auto_increment Primary Index
name text No
 
The line after $sql you'll want to put this:

$result = mysql_query($sql,$dbh) or die(&quot;Information insersion fail.<br>Reason: &quot;. mysql_error());



I believe this should fix your problem.
 
I found this also strange
$sql = &quot;INSERT INTO user (id, name) VALUES ('', '$_POST[name]')&quot;;

if id is an autoincrement you can leave it out
Code:
$sql = &quot;INSERT INTO user (name) VALUES ('$_POST[name]')&quot;;

 
Not sure if user is a keyword in mysql.

Try renaming table user to user1 or something and try..



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
take a look on this:


$dbh=mysql_connect(&quot;localhost&quot;, &quot;panao3_matt&quot;, &quot;Kiani123&quot;) or die ('I cannot connect to the database because: ' .

...

//create SQL statement
$sql = &quot;INSERT INTO user (id, name) VALUES ('', '$_POST[name]')&quot;;

// execute the SQL statement
if (mysql_query($sql, $conn)) ...

you use $dbh to make the connection, but in mysql_query you are using $conn ...!!?? does it make sense for you?

and it should by mysql_query($dbh, $sql).

hope this helps you.
 
Thank everybody very much for your help. It was driving me crazy. I was so out of it I even left my username and passwords in the script. Here is a copy of what I have now and it works if anybody is interested.

<?php
//open connection

$dbh=mysql_connect(&quot;localhost&quot;, &quot;user&quot;, &quot;password&quot;) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db(&quot;panao3_tierii&quot;);

//create SQL statement
$sql = &quot;INSERT INTO tieriiuser VALUES ('', '$_POST[name]')&quot;;
$result = mysql_query($sql,$dbh) or die(&quot;Information insersion fail.<br>Reason: &quot;. mysql_error());


// execute the SQL statement
if (mysql_query($sql,$dbh)) {
echo &quot;record added!&quot;;
} else {
echo &quot;there is a trouble2&quot;;
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top