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!

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

<?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)

Link to comment
Share on other sites

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

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.