使用来自另一个表php的键更新表

时间:2019-01-08 03:00:43

标签: php mysql mysqli

我的数据库中有三个表,分别为sortingdynamic_textboxdynamic_optionlist。每个表都有一个键列,该列将用作主键。我需要更新排序表中的某些列。但是,每当我在where条件中使用排序键时,查询始终会出错。我有一个var_dump来检查我是否正在获取密钥,这是一个示例:

array(7) { ["key"]=> string(1) "4" 
           ["purpose"]=> string(14) "testing1000112"
           ["status"]=> string(4) "Open" 
           ["remarks"]=> string(13) "ZXCqweriopasd" 
           ["Update"]=> string(6) "Update" 
         }

请帮助我进行查询,因为每次我单击“更新”按钮时,所有条目有时都会更新,或者根本不会更新。我还想将dynamic_optionlist表中的键用作where条件中的键。甚至有可能吗?

//request.php

<?php
	$con= mysqli_connect ('localhost', 'root', 'mypassword', 'mydatabase');
	
	$info= "SELECT dynamic_optionlist.key, dynamic_optionlist.destination, sorting.requestor, sorting.purpose, dynamic_optionlist.criteria, sorting.request_date, sorting.status, sorting.updated_by, sorting.remarks
		FROM ((sorting 
		INNER JOIN dynamic_optionlist ON sorting.rnd_num = dynamic_optionlist.rnd_num)
		INNER JOIN dynamic_textbox ON dynamic_optionlist.key = dynamic_textbox.key) WHERE status in ('Open','On-Going','On-Queue')";

	$queryres = mysqli_query($mcw,$getjobinfo);	
	

	while ($rowwaf = mysqli_fetch_assoc($queryres))
	{
		echo "<FORM action=\"update.php\" method=\"POST\">\n 
		echo "<td><input type=\"hidden\" name=\"key\" value=\"".$rowwaf["key"]."\">";
		echo "<td class=\"data\">".$rowwaf["key"]."</td>\n" ;
		echo "<td class=\"data\">".$rowwaf["destination"]."</td>\n" ;
		echo "<td class=\"data\">".$rowwaf["requestor"]."</td>\n" ;
		echo "<td class=\"data\"><input size=\"50\" TYPE=\"text\" NAME=\"purpose\" value=\"".$rowwaf["purpose"]."\" required></td>\n" ;
		echo "<td class=\"data\">".$rowwaf["criteria"]."</td>\n" ;
		echo "<td class=\"data\">".$rowwaf["request_date"]."</td>\n" ;
		echo "<td><select  name=\"status\">\n";
				/*display current value -> selected*/
					echo "\t<option value=\"Open\" ";
						if($rowwaf["status"] == "Open"){echo "selected";}
					echo ">Open</option>\n";
					
					echo "\t<option value=\"On-Queue\" ";
						if($rowwaf["status"] == "On-Queue"){echo "selected";}
					echo ">On-Queue</option>\n";	
									
					echo "\t<option value=\"On-Going\" ";
						if($rowwaf["status"] == "On-Going"){echo "selected";}
					echo ">On-Going</option>\n";	
					
					echo "\t<option value=\"Done\" ";
						if($rowwaf["status"] == "Done"){echo "selected";}
					echo ">Done</option>\n";
					
					echo "\t<option value=\"Delete\" ";
						if($rowwaf["status"] == "Delete"){echo "selected";}
					echo ">Delete</option>\n";
					
			echo "</select></td>\n";	
		
		echo "<td class=\"data\">".$rowwaf["updated_by"]."</td>\n" ;
		echo "<td class=\"data\"><input name=\"remarks\" type=\"text\" size=\"10\" value=\"".$rowwaf["remarks"]."\" required>&nbsp</td>\n" ;
		echo "<td class=\"data\"><input type=\"SUBMIT\" size=\"5\" NAME=\"Update\" VALUE = \"Update\"></td>\n</tr>\n</form>\n" ;
		
	}

?>






//update.php

  <?php
$con= mysqli_connect ('localhost', 'root', 'mypassword', 'mydatabase');

$key=$_POST["key"];
$rem=$_POST["remarks"];
$pur=$_POST["purpose"];
$sta=$_POST["status"];

$updquery = "UPDATE table1
	SET status = '$STATUS', remarks='$REMARKS', purpose='$PURPOSE' WHERE key = '$key'";

$result = mysqli_query($con,$updquery);
	if ($result) {
			$info= "SELECT dynamic_optionlist.key, dynamic_optionlist.destination, sorting.requestor, sorting.purpose, dynamic_optionlist.criteria, sorting.request_date, sorting.status, sorting.updated_by, sorting.remarks
		FROM ((sorting 
		INNER JOIN dynamic_optionlist ON sorting.rnd_num = dynamic_optionlist.rnd_num)
		INNER JOIN dynamic_textbox ON dynamic_optionlist.key = dynamic_textbox.key) WHERE key = '$key'";

$queryres = mysqli_query($con,$info);	
$rowwaf = mysqli_fetch_assoc($queryres);

		if($STATUS =="Update"){
			echo "
			\n	
			<table class=\"table\" cellpadding=\"5\" rules=\"all\" frame=\"box\" border=\"1\">
			<tr><td class=\"data\"><strong>SR_NO:</strong></td><td>SR-".$rowwaf["key"]."</td></tr>
			<tr><td class=\"data\"><strong>DESTINATION:</strong> </td><td>".$rowwaf["destination"]."</td></tr>
			<tr><td class=\"data\"><strong>REQUESTOR:</strong> </td><td>".$rowwaf["requestor"]."</td></tr>
			<tr><td class=\"data\"><strong>PURPOSE:</strong> </td><td>".$rowwaf["purpose"]."</td></tr>
			<tr><td class=\"data\"><strong>SAMPLE_CRITERIA:</strong> </td><td>".$rowwaf["criteria"]."</td></tr>
			<tr><td class=\"data\"><strong>REQUEST_DATE:</strong> </td><td>".$rowwaf["request_date"]."</td></tr>
			<tr><td class=\"data\"><strong>STATUS:</strong> </td><td>".$rowwaf["status"]."</td></tr>
			<tr><td class=\"data\"><strong>REMARKS:</strong> </td><td>".$rowwaf["remarks"]."</td></tr>
			</table>
			";
}
	else {
		echo "<HR>
		<p style=\"color:red\">Error on updating</p>
		<HR>";
		var_dump($_POST);
	}	
	
?>

1 个答案:

答案 0 :(得分:-1)

我猜间歇更新的问题在于您的数据。尝试使用escape_string。

$con= mysqli_connect ('localhost', 'root', 'mypassword', 'mydatabase');

$key=mysqli_real_escape_string($_POST["key"]);
$rem=mysqli_real_escape_string($_POST["remarks"]);
$pur=mysqli_real_escape_string($_POST["purpose"]);
$sta=mysqli_real_escape_string($_POST["status"]);