Forum Moderators: coopster

Message Too Old, No Replies

INSERT into seems not working.

         

afridy

5:49 am on Apr 16, 2006 (gmt 0)

10+ Year Member



Hi. i am new to mysql. problem is i cant crete records.

//add records
$sql = "INSERT INTO users (`email`, `name`, `country`) VALUES ('john@gmail.com', 'john', 'non')";
if (!$sql){
dir ("Unable to create the record:" . mysql_error());
exit;
}

question : The above code is not creating the specified record.
currently i have 2 records already in globle_users db.
bought added manually using myphpadmin.

here is the complete code.

<?php
$link = mysql_connect('localhost', 'globe_sql', '123456');
if (!$link) {
die('Could not connect to mysql server: ' . mysql_error());
}
echo "Connected to mysql successfully.<br>". "\n";

//select db
mysql_select_db ("globe_users") or die ("But could not select the specified db!<br>");

//print data
// Select all rows in the users table

$query = "SELECT * FROM users";
$result = mysql_query($query);
if (!$result) {
die ("Query made to table named users was un-successfull");
}

print "Query made to the users table was successfull<br>" . "\n";

$x = 0;
//preparing the table
print "<table>";
print "<tr><th>Email</th><th>Name</th></tr>";
print "<tr><th>-----</th><th>----</th><tr>";

//looping to print rows

while ($x < mysql_numrows($result)): //$result is returned by mysql_query
$mail=mysql_result($result, $x, 'email');
$name=mysql_result($result, $x, 'name');
print"<tr>";
print"<td>$mail</td><td>$name</td>";
print"</tr>";
$x++;
endwhile;
print"</table>";

//add records
$sql = "INSERT INTO users (`email`, `name`, `country`) VALUES ('john@gmail.com', 'john', 'non')";
if (!$sql){
dir ("Unable to create the record:" . mysql_error());
exit;
}

mysql_close($link);

?>

Habtom

5:57 am on Apr 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



//$sql = "INSERT INTO users (`email`, `name`, `country`) VALUES ('john@gmail.com', 'john', 'non')";
if (!$sql){
dir ("Unable to create the record:" . mysql_error());
exit;
}

You got to execute it. Put the following line, just after the query

mysql_query($sql);

. . . and you shall be fine

:)

Habtom

afridy

6:46 am on Apr 16, 2006 (gmt 0)

10+ Year Member



Hai habtom.

that it. works very well. i am very very happy.

Best regards
afridy.