Jump to content


Photo

PHP & MySQL 'INSERT INTO' function not working.


  • Please log in to reply
2 replies to this topic

#1 nickholt1972

nickholt1972
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationBury, Lancs, UK

Posted 05 August 2006 - 03:05 PM

Here's a snippet of the code i'm working on (for info, the page which feeds the data to this one is a basic form which gathers all the required info):

$productid = $_REQUEST['productid'];
$alphaname = $_REQUEST['alphaname'];
$alphatype = $_REQUEST['alphatype'];
$subcat = $_REQUEST['subcat'];
$manufacturer = $_REQUEST['manufacturer'];
$size = $_REQUEST['size'];
$colour = $_REQUEST['colour'];
$pattern = $_REQUEST['pattern'];
$fastening = $_REQUEST['fastening'];
$description1 = $_REQUEST['description1'];
$description2 = $_REQUEST['description2'];
$description3 = $_REQUEST['description3'];
$smlimage = $_REQUEST['smlimage'];
$lgeimage = $_REQUEST['lgeimage'];
$addbasket = $_REQUEST['addbasket'];
$price = $_REQUEST['price'];
$display = $_REQUEST['display'];

echo '<b>Product ID: </b>' . $productid . '<br />';
echo '<b>AlphaName: </b>' . $alphaname. '<br />';
echo '<b>AlphaType: </b>' . $alphatype. '<br />';
echo '<b>Subcat: </b>' . $subcat. '<br />';
echo '<b>Manuf: </b>' . $manufacturer. '<br />';
echo '<b>Size: </b>' . $size. '<br />';
echo '<b>Colour: </b>' . $colour. '<br />';
echo '<b>Pattern: </b>' . $pattern. '<br />';
echo '<b>Fast: </b>' . $fastening. '<br />';
echo '<b>Desc: </b>' . $description1. '<br />';
echo '<b>Desc: </b>' . $description2. '<br />';
echo '<b>Desc: </b>' . $description3. '<br />';
echo '<b>Sml Image: </b>' . $smlimage. '<br />';
echo '<b>Lge Image: </b>' . $lgeimage. '<br />';
echo '<b>Add Basket Text: </b>' . $addbasket. '<br />';
echo '<b>Price: </b>' . $price. '<br />';
echo '<b>Display: </b>' . $display. '<br /><br /><br />';


$insertdata = "INSERT INTO stocks SET 
	productid='$productid',
	alphatype='$alphatype',
	alphaname='$alphaname',
	subcat='$subcat',
	manufacturer='$manufacturer',
	size='$size',
	colour='$colour',
	pattern='$pattern',
	fastening='$fastening',
	description1='$description1',
	description2='$description2',
	description3='$description3',
	smlimage='$smlimage',
	lgeimage='$lgeimage',
	addbasket='$addbasket',
	price='$price',
	display='$display'";

if (!$insertdata) {
  exit('<p>Unable to insert data:</p>');
}

So what's happening is, the top section of code gathers the data from the input form and I know this is working because the second section of code displays it on the page perfectly. The problem is with the final section of code which should just pop the data into my database. All that happens is the data displays on the page (so there's no syntax errors) but when I go to my display data page, there's nothing there.
All my connect settings are fine, it just something wrong with the INSERT INTO .... command.
Can anyone help, i'm really at a loss now.
Thanks,
Nick Holt.

Nick Holt - Future Guru

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 05 August 2006 - 03:09 PM

You are not using mysql_query to perform the query. You should do this:
mysql_query($insertdat) or die("<p>Unable to insert data:</p>');
Instead of:
if (!$insertdata) {
  exit('<p>Unable to insert data:</p>');
}
Also make sure you are connect to MySQL too.

#3 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 05 August 2006 - 03:35 PM

And to save you a lot of wasting time while typing I suggest you use:

// This comes on top of your script or wherever you want your $productid, and everything to start
extract($_REQUEST);

// Now will this still work
echo '<b>Product ID: </b>' . $productid . '<br />';
echo '<b>AlphaName: </b>' . $alphaname. '<br />';
echo '<b>AlphaType: </b>' . $alphatype. '<br />';
echo '<b>Subcat: </b>' . $subcat. '<br />';
echo '<b>Manuf: </b>' . $manufacturer. '<br />';
echo '<b>Size: </b>' . $size. '<br />';
echo '<b>Colour: </b>' . $colour. '<br />';
echo '<b>Pattern: </b>' . $pattern. '<br />';
echo '<b>Fast: </b>' . $fastening. '<br />';
echo '<b>Desc: </b>' . $description1. '<br />';
echo '<b>Desc: </b>' . $description2. '<br />';
echo '<b>Desc: </b>' . $description3. '<br />';
echo '<b>Sml Image: </b>' . $smlimage. '<br />';
echo '<b>Lge Image: </b>' . $lgeimage. '<br />';
echo '<b>Add Basket Text: </b>' . $addbasket. '<br />';
echo '<b>Price: </b>' . $price. '<br />';
echo '<b>Display: </b>' . $display. '<br /><br /><br />';

// What others did not notice is that your query is also wrong.
$insertdata = "`productid`, `alphatype`, etc...";
$query = "INSERT INTO stocks ( %s ) VALUES ( %s )";
// if you are inserting respectively with the columns, you can also use $query = "INSERT INTO stocks VALUES ( %s )";
// ofcourse this way you don't need $insertdata

// Also this is alot of sql code to parse, so check first what is set and what is not!
$value = '';
foreach ($_REQUEST as $k => $v) {
   if ($v) {
      $value .= "'" . $v . "',";
   }
}

// Then to make sure that we don't have a , as last character in our values variable
if (substr($value, -1) == ',') {
  $value = substr($value, 0, strlen($value)-1);
}

$Sql = sprintf($query, $insertdata, $value);

// For your DB and everything, you should use something lik
if (!($Resource = mysql_connect($Host, $User, $Pass))) {
   die('could not connecto to datbase');
}
if (!mysql_select_db($Database)) {
   die('selecting database failed.');
}
// Then parsing your query
if (!mysql_query($Sql, $Resource)) {
   echo "query failed";
} else {
   echo 'jipii';
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users