Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

[HELP][PHP] prepared queries/if statement

levgnzls

Recruit
Basic Member
Messages
4
Reaction score
0
Points
16
mga sir patulong naman. ang gusto ko kasing mangyari ay saka lang magrun si query2 if !$success, so far nageexecute parehong query


Code:
<?php

require 'dbconfig.php';
$conn    = Connect();
$name    = $conn->real_escape_string($_POST['u_name']);
$contact   = $conn->real_escape_string($_POST['u_contact']);
$branch = $conn->real_escape_string($_POST['branch']);
/**$query   = "INSERT into tb_user (u_name,u_contact,branch) VALUES('" . $name . "','" . $contact . "','" . $branch . "')";**/
$success = $conn->query($query);
$query1   = $conn->prepare("INSERT into tb_user (u_name,u_contact,branch) VALUES(?,?,?)");

$query1->bind_param("sis" ,$name, $contact, $branch);
$query1->execute();

$query2 = $conn->prepare("INSERT INTO tb_prize(prize) VALUES (?)");
$query2->bind_param("s", $prize);

$winningSegment = 'reg error';

if (!$success) { 
$query2->execute();
die("Couldn't enter data: ".$conn->error);
}

$query1->close();
$query2->close();

echo "Thank you! <br>";

$conn->close();

echo "<script type='text/javascript'>  window.location='next.html'; </script>";

?>
 
Last edited:
Tama pa ba yan script mo?

Naka-comment pa itong si $query, pero si $success ay TRUE?
Code:
/**$query   = "INSERT into tb_user (u_name,u_contact,branch) VALUES('" . $name . "','" . $contact . "','" . $branch . "')";**/
 
Tama pa ba yan script mo?

Naka-comment pa itong si $query, pero si $success ay TRUE?
Code:
/**$query   = "INSERT into tb_user (u_name,u_contact,branch) VALUES('" . $name . "','" . $contact . "','" . $branch . "')";**/

para lang sana makita ko kung saan ako nagsimula before naging ganito. any possible alternatives? or wag ko na lang ipursue?
 
Last edited:
The thing is, since commented out si $query and still nag success, meaning nagkaroon ng value si $query, kasi kung hindi, definitely may error yan. Either search for the part na nagkaroon ng ibang value si $query OR post the entire script here.
 
The thing is, since commented out si $query and still nag success, meaning nagkaroon ng value si $query, kasi kung hindi, definitely may error yan. Either search for the part na nagkaroon ng ibang value si $query OR post the entire script here.

so dapat ganito na lang sir? ang gusto ko lang naman mangyari sana ay tuwing magkaproblem lang si user sa registration saka magrun si query2 pero ang nangyari nararun pa din both queries pero instead na magredirect kay 'next.html' nastuck na lang sa echo message
Code:
<?php

require 'dbconfig.php';
$conn    = Connect();
$name    = $conn->real_escape_string($_POST['u_name']);
$contact   = $conn->real_escape_string($_POST['u_contact']);
$branch = $conn->real_escape_string($_POST['branch']);
$success = $conn->query($query2);
$query1   = $conn->prepare("INSERT into tb_user (u_name,u_contact,branch) VALUES(?,?,?)");
$query1->bind_param("sis" ,$name, $contact, $branch);
$query1->execute();

$query2 = $conn->prepare("INSERT INTO tb_prize(prize) VALUES (?)");
$query2->bind_param("s", $prize);

$success = $conn->query($query2);
$prize = 'reg error';

if (!$success) { 
$query2->execute();
die("Couldn't enter data: ".$conn->error);
}

$query1->close();
$query2->close();

echo "Thank you! <br>";

$conn->close();

echo "<script type='text/javascript'>  window.location='next.html'; </script>";

?>
 
so dapat ganito na lang sir? ang gusto ko lang naman mangyari sana ay tuwing magkaproblem lang si user sa registration saka magrun si query2 pero ang nangyari nararun pa din both queries pero instead na magredirect kay 'next.html' nastuck na lang sa echo message
Code:
<?php

require 'dbconfig.php';
$conn    = Connect();
$name    = $conn->real_escape_string($_POST['u_name']);
$contact   = $conn->real_escape_string($_POST['u_contact']);
$branch = $conn->real_escape_string($_POST['branch']);
$success = $conn->query($query2);[COLOR="#FF0000"] // at this time, wala pa value si $query2, you are passing whatever the result of this operation to $success[/COLOR]
$query1   = $conn->prepare("INSERT into tb_user (u_name,u_contact,branch) VALUES(?,?,?)");
$query1->bind_param("sis" ,$name, $contact, $branch);
$query1->execute();

$query2 = $conn->prepare("INSERT INTO tb_prize(prize) VALUES (?)");
$query2->bind_param("s", $prize);

$success = $conn->query($query2); // may value na dito si $query2, then ipapasa yung result ng operation sa $success, overwriting the previous result 
$prize = 'reg error';

if (!$success) { [COLOR="#FF0000"]// then ine-evaluate mo ngayon ung value ni $success, and from the looks of it, definitely !$success nga, kaya ie-execute pa din yung 2nd query[/COLOR]
$query2->execute();
die("Couldn't enter data: ".$conn->error);
}

$query1->close();
$query2->close();

echo "Thank you! <br>";

$conn->close();

echo "<script type='text/javascript'>  window.location='next.html'; </script>";

?>

The message you have entered is too short. Please lengthen your message to at least 10 characters daw.
 
The message you have entered is too short. Please lengthen your message to at least 10 characters daw.

Kung tama intindi ko sir, dapat ganito si $success? Tawagin nya muna si query 1 and if nagfail, saka iexecute si query 2?
$success = $conn->query($query1);
 
Gamitan mo ng rollback at commit, make changes or not in the database.
Ganito ang common strategy pag maraming insert like batch insert; pag isa ang failed lahat hindi nya e insert. mag cocommit kalang kapag lahat ng query mo success at the end.

USE try and catch

or

kahit simpling if else,


if($query1) #### if query 1 Succeed to execute
{
#dito mo gawin query2
if($query2) #### if query 2 Succeed to execute
{
#make changes in the database mean the query will now be inserted
$query->commit();

echo "Thank you! <br>";


#hindi na cguro kailangan ng close sa query mo. redunduncy nayan kung mag ko close kalang din naman ng buong connection mo $conn->close();

$query1->close(); --kahit wala na ito
$query2->close(); --kahit wala na ito

$conn->close(); --kasi dito mawawala din naman sila. ito nalang gamitin mo.

echo "<script type='text/javascript'> window.location='next.html'; </script>";
}
else #### if query 2 failed to execute
{
#make no changes in the database mean the query will not be inserted
$query->rollback();

#dito ka naman maglagay ng response either echo or die();

echo json_encode(array ("error"=>"There was an error in query 2"));
or
die("There was an error in query 1");
}

}

else #### if query 1 failed to execute
{

#make no changes in the database mean the query will not be inserted
$query->rollback();

#dito ka naman maglagay ng response either echo or die();

echo json_encode(array ("error"=>"There was an error in query 1"));
or
die("There was an error in query 1");
}
 
Last edited:
if (!$success) {

// lagay mo d2 lahat ng $query2
//pati ito $query2->close();
echo "Failed"; <-- lagay ka error messages para ma laman mo kng saan banda nagka error
}else{
$query1->close();
echo "Success"; <-- lagay ka error messages para ma laman mo kng saan banda nagka error
}
 
Last edited:
mga sir patulong naman. ang gusto ko kasing mangyari ay saka lang magrun si query2 if !$success, so far nageexecute parehong query


Code:
<?php

require 'dbconfig.php';
$conn    = Connect();
$name    = $conn->real_escape_string($_POST['u_name']);
$contact   = $conn->real_escape_string($_POST['u_contact']);
$branch = $conn->real_escape_string($_POST['branch']);
/**$query   = "INSERT into tb_user (u_name,u_contact,branch) VALUES('" . $name . "','" . $contact . "','" . $branch . "')";**/
$success = $conn->query($query);
$query1   = $conn->prepare("INSERT into tb_user (u_name,u_contact,branch) VALUES(?,?,?)");

$query1->bind_param("sis" ,$name, $contact, $branch);
$query1->execute();

$query2 = $conn->prepare("INSERT INTO tb_prize(prize) VALUES (?)");
$query2->bind_param("s", $prize);

$winningSegment = 'reg error';

if (!$success) { 
$query2->execute();
die("Couldn't enter data: ".$conn->error);
}

$query1->close();
$query2->close();

echo "Thank you! <br>";

$conn->close();

echo "<script type='text/javascript'>  window.location='next.html'; </script>";

?>

bro try mo yung ganto(intindihin mo nalang)
if($query1 == true){
Run Query2 Here
} else {
condition here
}


kasi bos base sa code mo, bago kapa nag if statement e nagexecute na agad mga queries, useless lang yung if statement mo jan, dapat sa loob ng if statemement mo doon lahat ng query2 (correct me if im wrong, beginner here)
 
Last edited:
Back
Top Bottom