Jump to content

Extract data from $row into database. Simple thing i think...


cs.punk

Recommended Posts

??? ??? ???

 

Ok heres the code

<?php
  
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("firstdatabase", $con);


$query = "SELECT * FROM shop_stock WHERE Item = ('$_POST[item]') ";
$result = "mysql_query($con,$query)"
or die ("Couldnt execute query select and where query.");

$row = "mysql_fetch_array($result)";


$sql = "INSERT INTO homepage_special(Item, Amount, ID)
VALUES
( ($row['Item']),($row['Amount']),($row['ID']) )";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record has been sucsessfuly added!";


?>

 

 

What exactly must I insert into the

 

$sql = "INSERT INTO homepage_special(Item, Amount, ID)
VALUES
( ($row['Item']),($row['Amount']),($row['ID']) )";

 

?

Link to comment
Share on other sites

First off, you do not put functions inside quotes:

 

$result = "mysql_query($con,$query)"
$row = "mysql_fetch_array($result)";

 

Both should be:

$result = mysql_query($con,$query)
$row = mysql_fetch_array($result);

 

Second when using variables inside of strings that are arrays:

$query = "SELECT * FROM shop_stock WHERE Item = '{$_POST[item]}' ";

 

You use { } to surround them.

 

Fix those issues and then see if you still have that question.

Link to comment
Share on other sites

Second when using variables inside of strings that are arrays:

$query = "SELECT * FROM shop_stock WHERE Item = '{$_POST[item]}' ";

 

You use { } to surround them.

 

Fix those issues and then see if you still have that question.

 

Huh.  Never knew about that shortcut, I always assigned them to new vars just to be safe.

Link to comment
Share on other sites

Second when using variables inside of strings that are arrays:

$query = "SELECT * FROM shop_stock WHERE Item = '{$_POST[item]}' ";

 

You use { } to surround them.

 

Fix those issues and then see if you still have that question.

 

Huh.  Never knew about that shortcut, I always assigned them to new vars just to be safe.

 

Yea I did not point that his code is very prone to SQL Injection without escaping $_POST.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.