Problem with "status updates"

Messages
89
Reaction score
0
Points
6
Hello,

I have created a PHP code for status updates of members of my site.

It's running perfectly in my computer (localhost; WAMP). But it's not working when I am uploading it in my x10hosting account.

The code is as follows:

Code:
<?php

$pm_ops1 = mysql_connect("localhost","username","password");
$pm_conn1 = mysql_select_db("databaseName");
    

    //Hidden steps to obtain UserID, stored in $userID variable
    
@$statusCont = htmlentities($_POST["wall"]);  //$_POST["wall"] gets the actual contents of the status update from main.php page using POST method
@$statusCont = mysql_real_escape_string($statusCont);


// START updating database

$pm_query1 = "SELECT max(statusID) FROM statusTable";
$result1 = mysql_query($pm_query1);
$output = mysql_fetch_row($result1);
$output[0]++;
$statid = $output[0];

$updateStat = mysql_query("INSERT INTO statusTable (statusID, statup, uid, timestamp) VALUES ('".$statid."', '".$statusCont."', '".$userID."', '".date('ymdHis')."')");

// END updating database

mysql_close($pm_ops1);

header("location:main.php");

?>
The problem is that, whenever I am entering a new "Status" in the main.php page, the status ID ( $statid ) is not getting incremented. As a result, whenever I am editing/ deleting a particular status, all the status updates (which have the same status ID) are getting updated/ deleted.

The strange thing is that no such problem arises when I am using the same code in my own computer (localhost; WAMP server).

Can anyone tell me what is wrong?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Instead of incrementing the ID in your script (which could lead to a race condition where two instances of the script executes nearly simultaneously, giving the same ID to two different status updates), declare the statusID column as AUTO_INCREMENT and UNSIGNED.

Code:
ALTER TABLE statusTable MODIFY statusID INT UNSIGNED AUTO_INCREMENT NOT NULL;

Also, the "Table" in "statusTable" is redundant. It should be named simply "statuses".

Code:
ALTER TABLE statusTable RENAME TO statuses;

Lastly, there's no good reason to use the outdated mysql extension for new projects. Use mysqli or PDO and prepared statements.
 
Last edited:
Messages
89
Reaction score
0
Points
6
Instead of incrementing the ID in your script (which could lead to a race condition where two instances of the script executes nearly simultaneously, giving the same ID to two different status updates), declare the statusID column as AUTO_INCREMENT and UNSIGNED.
It worked. Thanks a lot!

"Table" in "statusTable" is redundant.
What does this mean? May I change the table's name to "status_table"?

Lastly, there's no good reason to use the outdated mysql extension for new projects. Use mysqli or PDO and prepared statements.
Thanks for the advice. But, unfortunately (for me), I am not familiar with MySQLi or PDO. :(

---------- Post added at 06:47 AM ---------- Previous post was at 06:43 AM ----------

By the way, I wanted to give a +rep to you, but I was shown this error message:
You must spread some Reputation around before giving it to misson again.
What does it mean?
 
Last edited:

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
PDO's not difficult to pick up, and the mysql extension is being deprecated (so your software will need to be updated before too long anyway).

And what misson means is that there's no good reason to include the word "table" in a table name. Each row represents an entity of some sort (in this case, a "status") and the table name should just be the plural of the entities it contains (in this case "statuses" (nested parenthetical comment: don't get cute and use the Latin plural)).
 
Top