Jump to content

[SOLVED] I need someone to check my code.


PhdJB

Recommended Posts

Im new at PHP, and i need someone to check my code for me:

 

 

 

<?

// connect to database //

$dbh = mysqli_connect($host, $username, $password, $db);

// turn off auto-commit

mysqli_autocommit($dbh, FALSE);

// end connection //

 

// run query 1

$result = mysqli_query($dbh, $query1);

if ($result !== TRUE) {

mysqli_rollback($dbh); // if error, roll back transaction

}

 

// run query 2

$result = mysqli_query($dbh, $query2);

if ($result !== TRUE) {

mysqli_rollback($dbh); // if error, roll back transaction

}

// end query //

 

 

echo "Submission Completed!";

 

// close transaction //

mysqli_close();

?>

 

and than i have a bunch of variables

Link to comment
Share on other sites

Well, my first recommendation would be to define your queries within an array and simply loop over the array instead of trying to write each query individually:

<?php
$queries = array(
'-- do some SQL stuff --',
'-- do some SQL stuff --',
'-- do some SQL stuff --'
);

foreach ($queries as $q) {
if (mysqli_query($dbh, $q) === FALSE) {
	// Error occurred
}
}
?>

 

I really don't think that rollback is what you're after, though, because if there is an error, the transaction was not completed. mysqli_rollback() rolls back the current transaction, but if it was not completed, there's no need to roll it back.

Link to comment
Share on other sites

Am I missing something? The sequence you have the code in will not work. You first try to run a series of queries beofre you connect to the database and before you define your queries.

 

Also, if all your variables really are in the form: Location01, Location02, Location03, etc. and all your queries are the same (for 1 - 17) I would really suggest putting everything into arrays and then you could do everything in a few lines of code by utilizing a loop:

 

for ($i=1; $i<17; $i++) {

 $query= "INSERT INTO $table[$i](Location[$i], Model[$i], Lens[$i], FPS[$i], Quality[$i], RRes[$i], REC[$i], Power[$i]) VALUES ("$Location[$i]", "$Model[$i]", "$Lens[$i]", "$FPS[$i]", "$Quality[$i]", "$RRes[$i]", "$REC[$i]", "$Power[$i]")";

 $result = mysqli_query($dbh, $query23);

 if ($result !== TRUE) {

   mysqli_rollback($dbh); // if error, roll back transaction

 }

}

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.