Problems With Double-Entry with update - php

I'm trying to set up an update query to add the last entry with the new entry but my new entry keeps doubling.
I keep looking for error but everything seems ok in not sure why the value of sum keeps doubling.
$sum ='1';
$sql = "update table set old = old +'$sum' where id='1'";
$query=mysqli_query($con,$sql);
if ($con->query($sql) === TRUE) {
echo '<script type="text/javascript">alert("A ok");</script>';
} else {
echo "Bigo Problem: " . $con->error;
}

Try This
$sum =1;
$sql = "update table set old = old +'$sum' where id=1";
if ($con->query($sql) == TRUE) {
echo '<script type="text/javascript">
alert ("A ok");
</script>';
} else {
echo "Bigo Problem: " . $con->error;
}
This is the reason for doubling sum because query exected twice by your code.
$query=mysqli_query($con,$sql);
if ($con->query($sql) === TRUE)
Remove one line and code will work fine.

issue with below two lines.
$query=mysqli_query($con,$sql);
if ($con->query($sql) === TRUE) {
Above two line make two entry.

The problem is that mysqli_query($con,$sql) executes your query....and then $con->query($sql) also executes your query.
So you are running the same query twice from two different commands.
You can just remove $query=mysqli_query($con,$sql); - it isn't needed.

Related

PHP echo break tag

In my PHP script, there are 3 echo, based on the conditionals
echo "Error"
echo "User already existed"
echo "Success"
One of the echo will be passed to my android apps, if (s.equals("Success")), where s is the echo message.
But the echo I got when the registration success is <br />, according to the logcat. For User already existed have no problem.
Since s is not Success, I can't start another activity which is depended on the string. Can anyone explain how the break tag got echoed? The registration information successfully inserted into database, though.
elseif ($roleID == "C") {
$sql6 = "SELECT runnerID FROM tr_customer WHERE customerID = '$newID'";
$check4 = mysqli_fetch_array(mysqli_query($con,$sql6));
if(!isset($check4)) {
// add into db
$customerID = $roleID . $newID;
$sql7 = "INSERT INTO tr_customer(customerID, name, phoneNo, locationID, roleID, email) VALUES ('$customerID', '$name', '$phoneNo', '$locationID', '$roleID', '$email')";
if(mysqli_query($con,$sql7)) {
echo "Success";
}
} else {
$newID = checkExist();
}
I would examine the code where the variable is defined. If you could post that part of the code as an edit then perhaps someone can review it for you as well. There is just not enough information here to discern where your error is coming from.
EDIT:
Consider changing the way you check for a successful update.
$result = mysqli_query($con,$sql7);
if(mysqli_num_rows($result) == 1){
echo "Success";
}

activate more than a user at once

I'm working on my project for my school, I made activating a user account work, if the admin want to activate one user at a time,
but whenever the admin wants to update multiple record at once, it won't work
I tried to include the update SQL inside a foreach loop, but it gave me an error: invalid argument for foreach()
pleas guys help me,
if(isset($_POST['activate'] )){
$impid = implode(',', $_POST['SSU']);
// for each .. to update more than one at once.
foreach($impid AS $i){
$sql = "UPDATE Accounts SET Activated = '".$_POST['activate']."' WHERE SSU IN('" . $i . "') ";
$result = mysqli_query($dbCIE, $sql) or die(mysqli_error($dbCIE));
}
// to test. if anything got effected..
if (mysqli_affected_rows($dbCIE) > 0) {
echo "<script type='text/javascript'> alert('Successfully Updated ')</script>";
}
else {
echo "<script type='text/javascript'> alert('Failed to Update ')</script>";
} // end the test.
}else{echo "<script type='text/javascript'> alert(' Select Whether to Activare Or Deactive ')</script>";}
}
} // end of first if $_post[update]
thank u guys, I fixed it, i just passed $_POST['SSU'] to my foreach and it worked.

Update statement not executing inside while loop

Please help me with this.
I have a php code which search first for the records that will be needed to inserted to another table
here is my code:
//search for split values (capacitors)
$capacitance =mysql_query("SELECT itemno, wwpn, SUBSTR(val, 1, LENGTH(val) / 2) as capacitor,
SUBSTR(val, LENGTH(val) / 2+1) as capasitance
FROM bom_csv WHERE boardnumber ='$board' and bom_csv.qty<>'' and bom_csv.qty !='qty';");
while($row =mysql_fetch_array($capacitance))
{
echo "<pre>";
echo $row['itemno'];
echo $row['capacitor'];
echo $row['capasitance'];
echo $row['wwpn'];
echo "</pre>";
$capacitor = $row['capacitor'];
$capacity =$row['capasitance'];
$adi_pn=$row['adi_pn'];
$itemno=$row['itemno'];
//insert into via update
$update =#mysql_query ("UPDATE bom_crunching SET capacitor ='$capacitor', capacitance ='$capacity' WHERE boardmodel ='$board' and adi_pn ='$adi_pn'");
if ($update)
{
echo "OKAY!";
}
else
{
echo "NOT OKAY!";
}
}
While executing it, I am not getting any errors. However, when I look at my query browser it doesn't have any data inserted.
The result I want is to insert all the records in the table via update statement because it has a default value of null.
Thanks in advance!
First step, change the line:
$update =#mysql_query ("UPDATE bom_crunching SET capacitor ='$capacitor', capacitance ='$capacity' WHERE boardmodel ='$board' and adi_pn ='$adi_pn'");
to:
$update =mysql_query ("UPDATE bom_crunching SET capacitor ='$capacitor', capacitance ='$capacity' WHERE boardmodel ='$board' and adi_pn ='$adi_pn'") or die( mysql_error() );
So NOW you will be informed what is the problem, than we can help you better.
;)

Wrong part of if statement being executed

When I delete a record from table, I have 8 columns in the table and if more than 8 is entered it must be showed that nothing was deleted. However every number that I give, I get the same response "deleted successfully". This is my code:
$query = "DELETE FROM `order` WHERE orderId = '$_POST[id]'" ;
$result = mysql_query($query);
if(! $result )
{
echo ("$_POST[id] not deleted !<a href='deleteorder.php'>Back</a>");
}
else
{
echo ("$_POST[id] deleted succesfully! <a href='deleteorder.php'>Back</a>");
}
$result will be a valid result, even when your query won't affect any rows. Use mysql_affected_rows() to check if you deleted anything.
$result = mysql_query($query);
if( mysql_affected_rows() == 0 )
{
echo ("$_POST[id] not deleted !<a href='deleteorder.php'>Back</a>");
}
else
{
echo ("$_POST[id] deleted succesfully! <a href='deleteorder.php'>Back</a>");
}
side note: the mysql_* functions are deprecated. Don't use them to write new code, especially when you are learning. Use mysqli_*or PDO instead.
You can use mysql_affected_rows
$query = "DELETE FROM `order` WHERE orderId = {$_POST[id]}" ;
mysql_query($query);
if(0 == mysql_affected_rows()){
echo ("$_POST[id] not deleted !<a href='deleteorder.php'>Back</a>");
}else{
echo ("$_POST[id] deleted succesfully! <a href='deleteorder.php'>Back</a>");
}
You are testing if your query ran successfully. It can be successful if it deletes one row or if it deletes none.
Use mysql_affected_rows to determine if you've deleted any rows.
(You'd be better moving to a more modern API though)

Query always returns NULL

So the problem is the $check variable is always NULL, even though there is already a record in the database that match the SELECT statement I have written, it's still null. I know it's simple enough but i can't find the error. I already tried using empty, isNull, ====, but the problem is the $check is always null. Why it is always null?.
$sql = "SELECT * FROM linkedin_lookup WHERE "
."`given-name`='{$table['given_name']}' && "
."`family-name`='{$table['family_name']}' && "
."`location`='{$table['location']}' && "
."`industry`='{$table['industry']}' && "
."`headline`='{$table['headline']}'";
$results = $db->query($sql);
$check = $results->num_rows;
if ($check):
echo "Record already exist";
else:
$sql = "INSERT INTO linkedin_lookup (`given-name`,`family-name`,`location`,`industry`,`headline`,`date_search`) "
."VALUES "
."('{$table['given_name']}',"
."'{$table['family_name']}',"
."'{$table['location']}',"
."'{$table['industry']}',"
."'{$table['headline']}',"
."'{$now}')";
$db->query($sql);
echo "Succesfully inserted record(s)" . '<br />';
endif;
UPDATE MY CODE TO REFLECT INPUTS, but still it accepts duplicate. Is my SELECT STATEMENT wrong?. I put && right so it;s supposed to scan all fields if the same.
That's because you call num_rows on the result of the query (same for fetch_assoc), NOT the array.
$results = $db->query($sql);
$check = $results->num_rows;
if( $check) {
echo "Record already exists";
}
else {
$sql = "INSERT.....";
}
Note that you could just try to insert it up front and check affected_rows - if it's zero then it faied to insert due to already existing. Note that you'll need a UNIQUE KEY on the table in the right fields.
If $results does infact contain something....and the query is infact working, which I dunno if it is....
You could just try this instead....
$db->query($sql);
$results = $db->fetch_assoc();
if (count($results) > 0):
//echo "Record already exist";
else:
Ok now it works, i think the class $db is not mysqli, because we are using a framework that is developed by the company. So my codes now as follows and It works.
$sql = "SELECT * FROM linkedin_lookup WHERE "
."`given-name`='{$table['given_name']}' && "
."`family-name`='{$table['family_name']}' && "
."`location`='{$table['location']}' && "
."`industry`='{$table['industry']}' && "
."`headline`='{$table['headline']}'";
$db->query($sql);
$check = $db->fetch_array();
if ($check):
echo "Record already exist";
else:
$sql = "INSERT INTO linkedin_lookup (`given-name`,`family-name`,`location`,`industry`,`headline`,`date_search`) "
."VALUES "
."('{$table['given_name']}',"
."'{$table['family_name']}',"
."'{$table['location']}',"
."'{$table['industry']}',"
."'{$table['headline']}',"
."'{$now}')";
$db->query($sql);
echo "Succesfully inserted record(s)" . '<br />';
endif;

Categories