I have here two different query. First query is for both table that has a same item_code, and second table is only for table 1 that has a item code. I add console.log to run a script on the other page. The process of my code is if the first query (query for both table) failed to execute, the second query (query for table 1 only) execute. Both query executing correctly but I received this error on console.log.
我这里有两个不同的查询。第一个查询是针对具有相同item_code的表,第二个表仅针对具有项目代码的表1。我添加console.log以在另一页上运行脚本。我的代码的过程是如果第一个查询(两个表的查询)都无法执行,第二个查询(仅查询表1)执行。两个查询都正确执行但我在console.log上收到此错误。
"
Notice: Trying to get property of non-object in C:\xampplite\htdocs\edit.php on line 21,23,25
0"
“注意:尝试在第21,23,25行的C:\ xampplite \ htdocs \ edit.php中获取非对象的属性”
The line 21,23 and 25 error is my if else statement. I need to get console.log 1
.
第21,23和25行错误是我的if else语句。我需要获得console.log 1。
Why is that?
这是为什么?
Any help will appreciated.
任何帮助将不胜感激。
Both table have the item code A000321
两个表都有项目代码A000321
table 1 | table 2
|
item_code | item_code
------------|--------------
A000321 | A000321
Only table 1 have the item code A000321
只有表1具有项目代码A000321
table 1 | table 2
|
item_code | item_code
------------|--------------
A000321 |
Update.php
<?php
$val1_id= $_POST['val1_id'];
$val1_item_copy= $_POST['val1_item_copy'];
$val1_catcode= $_POST['val1_catcode'];
$val1_itemcode= $_POST['val1_itemcode'];
$sql = $mysqli->query("
UPDATE code t1
INNER JOIN
app t2
ON t2.item_code= t1.item_code
SET t1.item = '$val1_item_copy',t2.item_name = '$val1_item_copy'
WHERE t1.item_code = '$val1_itemcode' AND t1.cat_code = '$val1_catcode';
")or die('There was an error running the query [' . $mysqli->error . ']');
if(($sql->num_rows)>= 1){
echo '1';
} else if(($sql->num_rows) == 0) {
$sql1 = $mysqli->query("UPDATE code SET item='$val1_item_copy' WHERE id='$val1_id'")or die('There was an error running the query [' . $mysqli->error . ']');
if(($sql1->num_rows)>= 1){
echo '1';
} else {
echo '0';
}
}
?>
2 个解决方案
#1
0
You're likely getting a "1" because the update query is successful. This does not mean it made any changes, just that it didn't crash.
您可能会获得“1”,因为更新查询成功。这并不意味着它做了任何改变,只是它没有崩溃。
You can check the affected records by checking
您可以通过检查来检查受影响的记录
printf("Affected rows (Non-Select): %d\n", $mysqli->affected_rows);
#2
0
I just edit some details. I used if($mysqli->affected_rows >= 1)
instead of if(($sql->num_rows)>= 1)
我只是编辑一些细节。我使用if($ mysqli-> affected_rows> = 1)而不是if(($ sql-> num_rows)> = 1)
if($mysqli->affected_rows >= 1) {
echo '1';
} else {
$sql1 = $mysqli->query("UPDATE code SET item='$val1_item_copy' WHERE id='$val1_id'")or die('There was an error running the query [' . $mysqli->error . ']');
if($mysqli->affected_rows >= 1) {
echo '1';
} else {
echo '0';
}
}
#1
0
You're likely getting a "1" because the update query is successful. This does not mean it made any changes, just that it didn't crash.
您可能会获得“1”,因为更新查询成功。这并不意味着它做了任何改变,只是它没有崩溃。
You can check the affected records by checking
您可以通过检查来检查受影响的记录
printf("Affected rows (Non-Select): %d\n", $mysqli->affected_rows);
#2
0
I just edit some details. I used if($mysqli->affected_rows >= 1)
instead of if(($sql->num_rows)>= 1)
我只是编辑一些细节。我使用if($ mysqli-> affected_rows> = 1)而不是if(($ sql-> num_rows)> = 1)
if($mysqli->affected_rows >= 1) {
echo '1';
} else {
$sql1 = $mysqli->query("UPDATE code SET item='$val1_item_copy' WHERE id='$val1_id'")or die('There was an error running the query [' . $mysqli->error . ']');
if($mysqli->affected_rows >= 1) {
echo '1';
} else {
echo '0';
}
}