Jump to content

Using arrays in queries


DeepakJ

Recommended Posts

It doesn't seem to work. This is my code.

 

 

<?php
$hostname = "localhost";
$username = "root";
$password = "98989lol";
$dbname = "licensinginformation";
$customerid = $_GET['user'];
$productid = $_GET['user1'];


mysql_connect($hostname, $username, $password) or DIE("Unable to connect to MySQL server $hostname");
$selected = mysql_select_db($dbname) or DIE("Could not select requested db $dbname");

$querya = "SELECT * FROM invoiceid WHERE customerid='$customerid'";
$aResult = mysql_query($querya);

while($row1=mysql_fetch_array($aResult)){
$invoicenum[]= $row1['invoicenum'];
}

$queryd = "SELECT * FROM productid WHERE invoicenum IN('" . implode("', '", $invoicenum) . "')";
$dResult = mysql_query($queryd) or die(mysql_error());
while($row2=mysql_fetch_array($dResult)){
if ($row2['productid']="")
{
	$tableid = $row2['tableid'];
	$queryc = "UPDATE productid SET productid='$productid' WHERE tableid = '$tableid'";
	mysql_query($queryc) or die(mysql_error());
	break;
}
}
echo "Data successfully entered."

Link to comment
Share on other sites

You can simplify greatly.

 

Also, edit your post above and remove your connection information.

 

$query = "SELECT invoicenum FROM invoiceid WHERE customerid = '$customerid'";
$result = mysql_query($query) or die(mysql_query());

while($row = mysql_fetch_array($result)){
$invoices[]= $row['invoicenum'];
} 

echo "The following invoices will be updated: " . implode(", ", $invoices);

$query = "UPDATE productid SET productid = " . $productid . " WHERE invoicenum IN(" . implode(", ", $invoices) . ") AND (productid = "" OR productid IS NULL)";
mysql_query($query) or die(mysql_error());

echo mysql_affected_rows() . " rows were updated by the last query<br /><br />" . $query;

Link to comment
Share on other sites

Thats not what I want to do though. I want to update the product id on ONE of the invoices once. Doesnt matter which, aslong as it has a null string. There is a preivous script which sets the product ID to null when it is purchased and this interface is for licensing. Invoicenum is irrelavent except for the fact that it connects productid and invoiceid.

Link to comment
Share on other sites

This is what I have so far but it doesn't work. Nothing happens. Do you see anything wrong with the loop?

<?php
$hostname = "localhost";
$username = "root";
$password = "98989lol";
$dbname = "licensinginformation";
$customerid = $_GET['user'];
$productid = $_GET['user1'];


mysql_connect($hostname, $username, $password) or DIE("Unable to connect to MySQL server $hostname");
$selected = mysql_select_db($dbname) or DIE("Could not select requested db $dbname");

$querya = "SELECT * FROM invoiceid WHERE customerid='$customerid'";
$aResult = mysql_query($querya);

while($row1=mysql_fetch_array($aResult)){
$invoicenum[]= $row1['invoicenum'];
}

$queryd = "SELECT * FROM invoiceid, productid WHERE invoiceid.invoicenum = productid.invoicenum && customerid= '$customerid'";
//queryd = "SELECT * FROM productid WHERE invoicenum IN('" . implode("', '", $invoicenum) . "')";
$dResult = mysql_query($queryd) or die(mysql_error());
$boolean = false;
while($row2=mysql_fetch_array($dResult)){
if ($row2['productid']="")
{
	$tableid = $row2['tableid'];
	$queryc = "UPDATE productid SET productid='$productid' WHERE tableid = '$tableid'";
	mysql_query($queryc) or die(mysql_error());
	echo "Data successfully entered.";
	$boolean = true;
	break;
}
}
if ($boolean=false){
echo "The customer does not have enough licenses.";
}



?>

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.