Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > New years resolution: To get serious with my programming! Anyone wanna help? :)
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 8th November 18:07
japruim
External User
 
Posts: 1
Default New years resolution: To get serious with my programming! Anyone wanna help? :)



Hi Everyone,

Happy New Year a week late!

I am trying to get more serious with my programming, I feel fairly
confident in my basic abilities except for one... Error checking.
That's what I'm trying to get figured out

I have a script, that I am using to connect to my database, read,
insert, delete or edit the records in there.

most of the time the script works perfectly, but on the occassion it
doesn't like when jupiters third moon aligns with uranus, I want the
user to be notified to take their head out of their ass...

What I have tried is this:

$querytest = "INSERT INTO current VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
if ($stmt = mysqli_prepare($link, $querytest)) {


mysqli_stmt_bind_param($stmt, 'ssssssssss', $FName, $LName, $Add1,
$Add2, $City, $State, $Zip, $XCode, $Record, $Reason);
//Add the record
mysqli_stmt_execute($stmt);
printf("Error: %d.\n", mysqli_stmt_errno($stmt));
printf("%d Row Inserted.\n", mysqli_stmt_affected_rows($stmt));


}

//Close the statement
mysqli_stmt_close($stmt);


that was pulled off of the php.net site (For the most part) and
adapted slightly to meet my needs, and obviously I edited too much of
it

If anyone has any ideas I would appreciate it. Even RTFM as long as $M
is defined

--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
japruim@raoset.com
  Reply With Quote


 


2 8th November 18:07
parasane
External User
 
Posts: 1
Default New years resolution: To get serious with my programming! Anyone wanna help? :)



And to you, as well.


Do not discuss myanus in any public forum, Jason. This will be
your final warning. ;-P

I'm not sure of your exact problem, to be honest, but this part of
the statement should probably be:

$querytest = "INSERT INTO current(field1,field2,field3,field4)";
$querytest .= " VALUES('value1','value2','value3','value4')";

Keep in mind that I only broke up the query line to avoid
convolution due to linebreaks.

--
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.
  Reply With Quote
3 8th November 18:07
jmays
External User
 
Posts: 1
Default New years resolution: To get serious with my programming!


What, if any, errors are given when the query doens't work? Are any of
the field in mysql set to be unique and are you trying to insert a new
row with some of the same information?

I dont see anything wrong right off the bat with the way you are
performing the task.

--
Jack Mays
  Reply With Quote
4 8th November 18:07
japruim
External User
 
Posts: 1
Default New years resolution: To get serious with my programming! Anyone wanna help? :)


As I said in the other e-mail, the query works just fine. But if for
some reason the insert doesn't succeed I want it to tell the user that
it didn't succeed and that they should call their friendly underpaid
under-appreciated network admin (AKA: ME )

--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
japruim@raoset.com
  Reply With Quote
5 8th November 18:07
japruim
External User
 
Posts: 1
Default New years resolution: To get serious with my programming! Anyone wanna help? :)


Actually, the problem isn't the query... the query works fine. What I
want is for if it doesn't work, I want it to tell the user that it
didn't work.

Right now I'm just blindly accepting that the insert succeeded, and I
want to get away from that!

Does that explain it a little better?

--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
japruim@raoset.com
  Reply With Quote
6 8th November 18:08
jochem
External User
 
Posts: 1
Default New years resolution: To get serious with my programming!


Jason Pruim schreef:


it's possible that the binding fails. check the return value of
mysqli_stmt_bind_param() and if an error status is returned log the error
and don't try to execute.


again check the return value of the function you called (you beginning
to see a pattern here with regard to error checking? ;-)

you only need to print out (or log) an error if one actually occurred.
additionally if mysqli_stmt_execute() returns an error code you can output
a nice userfriendly message log the cryptic mysql error message, etc somewhere.

e.g.

if (!mysqli_stmt_execute($stmt)) {
echo "SOMETHING BAD HAPPENED!";
error_log(mysqli_stmt_errno($stmt));
}

hope you get the idea.

you might want to output the actual error message (often more useful than a
number) and also output the values you we're trying to submit to the DB.

lastly consider logging to a file (e.g. error_log()) and log enough so that
you build up a store of error data that you can use to help you track problems
that are apparently cropping up occasionally


you should only close the statement if it was actually prepared okay in the first place

at it's most simple:

if ($stmt)
mysqli_stmt_close($stmt);
  Reply With Quote
7 8th November 18:08
maillists
External User
 
Posts: 1
Default New years resolution: To get serious with my


If you are simply wanting to do a check on whether the insert was successful
or not, then grab the id form the insert and check to see if it is there...

$success = mysql_insert_id();

If(!$success){
echo "no joy.. Insert failed.";
}

Because mysql_insert_id() acts on the last performed query, be sure to call
mysql_insert_id() immediately after the query that generates the value.


--
Stephen Johnson c | eh
The Lone Coder

http://www.thelonecoder.com
continuing the struggle against bad code

http://www.thumbnailresume.com
--
  Reply With Quote
8 8th November 18:08
parasane
External User
 
Posts: 1
Default New years resolution: To get serious with my programming! Anyone wanna help? :)


And you French-kiss your mother with that mouth?

Ah, I see. What you probably want is something like this:
<?
// blah, blah, blah.... mysqli_stmt_execute($stmt) or die(mysqli_error());
?>

--
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.
  Reply With Quote
9 8th November 18:08
japruim
External User
 
Posts: 1
Default New years resolution: To get serious with my programming! Anyone wanna help? :)


I wasn't able to make it work with mysqli_insert_id() but you did give
me the idea of using mysqli_stmt_error() instead. Which doesn't return
anything if it was successful... So a simple:

$check = mysqli_stmt_error();
if($check = "")
redirect;
}
else
{
exit();
}

works great! Thanks for your help!


--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
japruim@raoset.com
  Reply With Quote
10 8th November 18:08
japruim
External User
 
Posts: 1
Default New years resolution: To get serious with my programming! Anyone wanna help? :)


Only on special occasions... Besides, isn't that illegal in every
state but PA?

Tried to do that, and couldn't get it to work, but as I noted in
another e-mail, I found away that I think will work!

--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
japruim@raoset.com
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Php 5 forum, Apache forum, Iis forum, Functions forum, Classes forum, Librarys forum, Bugs forum, Postgres forum, Mysql forum, Paradox forum, Ms sql forum, Configurations forum, Php.ini forum, Problems forum, Scripting forum, Css forum, General forums, Off-topic talk, Links, Extra forums, Php


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666