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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert INTO Problem?

Status
Not open for further replies.

joelxez

Programmer
Apr 18, 2002
67
PH
Dear Experts,


I'm having an error, there's no data added...


Here's my PHP Code Below (add.php)
____________________________________________________________

<?php

$host = &quot;localhost&quot;;
$user = &quot;joel&quot;;
$pass = &quot;&quot;;
$database = &quot;personal&quot;;
$table = &quot;tblpersonal&quot;;

echo &quot;<B>This is a list of items I have in my table named $table</B> <hr>&quot;;

// Connecting to the Database
$connect = @mysql_connect($host, $user, $pass) or die(&quot;could not connect to server&quot;);

@mysql(&quot;$database&quot;,&quot;INSERT INTO $table VALUES(
'ID',
'Name',
'Address')&quot;);

// Query the Database
$result = @mysql_query(@mysql) or die(&quot;could not complete your query&quot;);


?>

____________________________________________________________

And here's my add.html


<html>

<head>

<title>ADD</title>
</head>

<body>

<form method=&quot;POST&quot; action =&quot;add.php&quot;>
<input type=&quot;text&quot; name=&quot;Name&quot; size=&quot;20&quot;></p>
<p><input type=&quot;text&quot; name=&quot;Address&quot; size=&quot;20&quot;></p>
<p><input name=&quot;ID&quot; size=&quot;20&quot; type = &quot;hidden&quot;></p>
<p><input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;><input type=&quot;reset&quot; value=&quot;Reset&quot; name=&quot;B2&quot;></p>
</form>

</body>

</html>

____________________________________________________________

Joel,
 
Well, I'm no expert, but I hope you'll accept my answer anyway ;-). You are doing this:

@mysql(&quot;$database&quot;,&quot;INSERT INTO $table VALUES('ID','Name','Address')&quot;);

That's incorrect. You must say which row the info goes into or it doesn't know what to do with the info. I'm not what you called the rows, but if they were called id, name, and address, it would look like this:

@mysql(&quot;$database&quot;,&quot;INSERT INTO $table('ID','Name','Address') VALUES ('$ID','$Name','$Address')&quot;);

That should work:).

Rick
If I have helped you just click the first link below to let me know :)
 
I see some fatal errors in the code. First of all, there is no such function as mysql. Also, mysql_query takes a string as argument. Your code should look something like this:
<?php

$host = &quot;localhost&quot;;
$user = &quot;joel&quot;;
$pass = &quot;&quot;;
$database = &quot;personal&quot;;
$table = &quot;tblpersonal&quot;;

echo &quot;<B>This is a list of items I have in my table named $table</B> <hr>&quot;;

// Connecting to the Database
$connection = @mysql_connect($host, $user, $pass) or die(&quot;could not connect to server&quot;);
@mysql_select_db($database, $connection);

$query = &quot;INSERT INTO $table VALUES(
'ID',
'Name',
'Address')&quot;);

// Query the Database
$result = @mysql_query($query) or die(&quot;could not complete your query&quot;);
?> //Daniel
 
Oh, I thought that was a function I hadn't learned yet! Can you do that like that? I thought you had to do this with the rows and their values:

$query = &quot;INSERT INTO $table ('ID','Name','Address') VALUES('$ID','$Name','$Address')&quot;);

// Query the Database
$result = @mysql_query($query) or die(&quot;could not complete your query&quot;);

That's how I usually do it and it works.

Rick If I have helped you just click the first link below to let me know :)
 
If you are inserting the values in the exact same order as they are listed in the database table, you don't need the list of fields to insert into. But if you are just inserting in, for example, 3 out of 4 fields where the field that you dont insert into is an auto-incrementing index, you would need the field list. //Daniel
 
daniels code has this error:
'Address')&quot;);
the second bracket is surplus ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top