Re: <Newbie Question> Can't insert values into MySQL DB via PHP

From: Date: Fri, 10 Feb 2006 23:59:56 +0000
Subject: Re: <Newbie Question> Can't insert values into MySQL DB via PHP
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-230240@lists.php.net to get a copy of this message
Jochem Maas wrote:
Duggles Temple wrote:
I am having a problem with a shop system. I can't add values into the MySQL DB via a PHP statement. The values are being transferred from one page to another (know that from the echo statement), but the SQL statement isn't working. The statement is as follows: $conn = mysql_connect($DBhost,$DBuser,$DBpass) or die('Unable to connect to database'); $t = $_GET['newdvdtitle']; $y = $_GET['newdvdyear']; $c = $_GET['newdvdcost']; $p = $_GET['newdvdpurchased']; @mysql_select_db("$DBName") or die("Unable to select database $DBName"); $sqladd = "INSERT INTO 'dvd' ('id', 'title', 'year','cost','purchased') VALUES ( NULL , '$t', '$y', '$c' , '$p' )"; echo $sqladd;
using echo to find out whats' in your variable is a good start, also take a look a print_r() and var_dump() - they given more feedback regarding passed in vars. it would have been useful if you had actually posted the output of the echo statement.
$result = mysql_query($sqladd);
try adding something like: if (!$result) {
    die(mysql_errno($conn) . ": " . mysql_error($conn) . "\n")
}
Very good habit to be into. Also, adding the query will help during the "debugging phase" of development. I generally use something like: mysql_query("$query") or die("Error in query: query was $query and MySql sez: ".mysql_error()); If, as the continuing thread suggests, the query is actually empty, the above would make that rather obvious right away. HTH, Kevin Kinsey -- Adler's Distinction: Language is all that separates us from the lower animals, and from the bureaucrats.

« previous php.general (#230240) next »