Forum Moderators: coopster

Message Too Old, No Replies

Adding data to table, no working

unable to add data to table

         

Lighthater

1:23 am on May 13, 2004 (gmt 0)

10+ Year Member



I'm having trouble with a script that is supposed to add data to my table. Hope you can help. It shows it on screen, but doesn't write to the database. :(

It'd be great if someone can tell me where I went wrong.

<?php
$name=$_POST['name'];
$link=$_POST['link'];
$email=$_POST['email'];

$location="localhost";
$username="username";
$password="password";
$database="clickomatic";
$conn = mysql_connect("$location","$username","$password");
if (!$conn) die ("Could not connect MySQL");

//Create the query
$sql = 'INSERT INTO koc ( id , link , name , email ) '
. ' VALUES ( \"\", \"$link\", \"$name\", \"$email\" );'
. ' ';

//Execute the query
$rs=mysql_query($sql,$conn);

//Confirm the added record details
if($rs) { echo ("Record added: $id $link $name $email"); }

echo "$name $link $email";
?>

jatar_k

1:31 am on May 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this to get the actual error from mysql, it will usually give you more insight to what the problem is. Another thing that helps as well is to echo the exact query to the screen.

//Execute the query
echo "<p>query: ",$sql,"<p>";
$rs=mysql_query($sql,$conn) or die (mysql_error() [ca.php.net]);

Birdman

1:36 am on May 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you have your SQL query messed up a bit. Too many quotes.

This one may fare better:

$sql = 'INSERT INTO koc ( id , link , name , email ) VALUES ( \"\", \"$link\", \"$name\", \"$email\" );';

I do it the opposite(quote-wise):

$sql = "INSERT INTO koc ( id , link , name , email ) VALUES ( '', '" . $link . "', '" . $name' . ", '" . $email . "' )";

Basically, everything between double-quotes is treated as a string and everything between the periods " . $here . " are your PHP vars.

Lighthater

2:01 am on May 13, 2004 (gmt 0)

10+ Year Member



Here is the error:

query: INSERT INTO koc ( id , link , name , email ) VALUES ( \"\", \"$link\", \"$name\", \"$email\" );

You have an error in your SQL syntax near '\"\", \"$link\", \"$name\", \"$email\" ); ' at line 1

-------------------------------------------------------
Here is the error using Birdman's query:

query: INSERT INTO koc ( id , link , name , email ) VALUES ( '', 'http://www.kingsofchaos.com/recruit.php?uniqid=e552ufub', 'Rodnak'', 'user@domain.com' )

You have an error in your SQL syntax near 'user@domain.com' )' at line 1

Lighthater

2:16 am on May 13, 2004 (gmt 0)

10+ Year Member



SOLUTION! Thank you guys, with your guidence, I found the fix. I'll post it here for the benefit of others.

Code for insert.php:
--------------------

<?php
$name=$_POST['name'];
$link=$_POST['link'];
$email=$_POST['email'];

$location="localhost";
$username="username";
$password="password";
$database="databasename";
$conn = mysql_connect("$location","$username","$password");
if (!$conn) die ("Could not connect MySQL");

//Select the database
$rs = @mysql_select_db("$database", $conn) or die (mysql_error());

//Create the query
$sql = "INSERT INTO koc ( id , link , name , email ) VALUES ( '', ' $link ', ' $name ', ' $email ' )";

//Execute the query
echo "<p>query: ",$sql,"<p>";
$rs=mysql_query($sql,$conn) or die (mysql_error());
//$rs=mysql_query($sql,$conn);

//Confirm the added record details
if($rs) { echo ("Record added: $id $link $name $email"); }

//This is old stuff that didn't work
//mysql_select_db($database,$conn) or die ("Could not open database");
//$query = "INSERT INTO table_name(id,name,link,email VALUES(\"\",\"$name\",\"$link\",\"$email\"))";

// $result=mysql_query($query);
// mysql_close();

echo "$name $link $email";
?>

Code for add.php:
--------------------

<html>
<head>
<title>Add your link</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="insert.php" method="post">
<table width="90%" border="0">
<tr>
<td width="7%">Name: </td>
<td width="93%"><input type="text" name="name" size="30"></td>
</tr>
<tr>
<td>Link: </td>
<td><input type="text" name="link" size="30"></td>
</tr>
<tr>
<td>Email: </td>
<td><input type="text" name="email" size="30"></td>
</tr>
</table>
<p>
<input type="submit" name="submit" value="Submit">
</p>
</form>
</body>
</html>