Jump to content

Stable & Best way to INSERT INTO mysql from PHP


nightkarnation

Recommended Posts

Ok here's my doubt...

 

Lets say i have the following code on php:

 

if ($action == "writeMyChat")
{
$time = time();
$chat=$_POST['myChat']; 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("learning") or die(mysql_error());

mysql_query("INSERT INTO `chat` (chat, messageTime) VALUES ('$chat', '$time')");
}

 

The values are actually coming from Flash...sometimes some data is missed and not stored on mysql ...

Is there a simple way to tell php or flash that "hey, it didnt save on mysql, loop again until it saves"

 

Any ideas?? thanx for the help in advance,

 

Cheers!

After the insert query, run a query requesting the same information.  If it's there you're fine, otherwise try again.

 

$result=mysql_query("SELECT messageTime FROM `chat` WHERE `chat`='$chat' AND `messageTime`='$time'");
if(mysql_num_rows($result)==1){
    //Good
}
else{
    //Do it again
}

<?php
//--mysql_connect stuff--//

function newentry($id) {
   $sqls = "update TABLE set ";
   $sqle = " where id=$id";
   
   mysql_query("insert into TABLE(id) values('$id')");
   mysql_query($sqls."VARRIABLE=VALUE".$sqle);
   mysql_query($sqls."VARRIABLE=VALUE".$sqle);
   mysql_query($sqls."VARRIABLE=VALUE".$sqle);
   mysql_query($sqls."VARRIABLE=VALUE".$sqle);
//--etc
}
?>

(on one of my php games the signup line was so long that mysql wouldn't handle it, so I had to resort to this method, it works though :P.. the reason for $sqls and $sqle is simply to save space\time when writing it)

Wouldn't it be better to check if the variables are empty or not?

 

<?php
if(!$chat && !$time){
     //variables empty, dont insert them
} else{
     //run the insert query
}
?>

I doubt $time will be empty or undefined.

 

<?php
//--mysql_connect stuff--//

function newentry($id) {
   $sqls = "update TABLE set ";
   $sqle = " where id=$id";
   
   mysql_query("insert into TABLE(id) values('$id')");
   mysql_query($sqls."VARRIABLE=VALUE".$sqle);
   mysql_query($sqls."VARRIABLE=VALUE".$sqle);
   mysql_query($sqls."VARRIABLE=VALUE".$sqle);
   mysql_query($sqls."VARRIABLE=VALUE".$sqle);
//--etc
}
?>

(on one of my php games the signup line was so long that mysql wouldn't handle it, so I had to resort to this method, it works though :P.. the reason for $sqls and $sqle is simply to save space\time when writing it)

You misspelled "variable". :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.