Log in

View Full Version : INSERT INTO query??



sniperman
07-17-2009, 07:45 AM
a newbie question here.

what is the correct syntax to use with PHP for an INSERT INTO query to a MySQL database that resides on the same server.

The server runs these versions


PHP version 5.2.6
MySQL version 5.0.77-community-log




<?php
$link = mysql_connect('localhost', 'admin1', 'admin2');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('database', $link);
if (!$db_selected) {
die('<p>unable to locate the database</p>');
}
/* Insertion Query to store attributes into Database */
mysql_query ("INSERT INTO tester
('value1', 'value2') VALUES ('tested', 'successfully')")
or die(mysql_error());

echo '<br />done successfully<br />';

mysql_close($link);

?>

The connection and database selection was successful. However I have labored for days on the right syntax to INSERT DATA with no luck, so i've turned to the forum for help. Thanks in advance.

fobos
07-18-2009, 08:59 PM
http://www.w3schools.com/php/php_mysql_create.asp

follow this and you cannot go wrong.

davelf
08-12-2009, 07:18 AM
include "connect.php";
$sql= "insert into tab_picture(picture,name) values ('$filename', '$name')";
mysql_query($sql);



sorry if i don't give the correct answer, but this is what i get form my class:)

JShor
08-12-2009, 02:05 PM
The code looks just fine, what exactly goes wrong? Do you get a syntax error/mysql error?