Database table not inserting any new row

Is database table insert working fine for you guys? My api which was tested and working fine before suddenly fails to insert new records to the table. I haven’t changed anything in my apis. They were working fine before.

mysql_query(“INSERT INTO db_name_users SET username = '”.$username."’, password = ‘".$password."’");
$id = mysql_insert_id();

the id is 0 and the table doesn’t show the row.

Any help would be much appreciated.

Can you access PHPmyadmin or does it not let you in?

Yes I can access it. The read operations are working fine like Login. Or another api which only read and return the data. I think there is some thing wrong with write operations like INSERT.

Have you exceeded 250 tables?

No I only have 1 db and 3 tables.

1 Like

I’ve opened a ticket to let a developer check on this.

In the meantime @ckhawand might have some ideas… :slight_smile:

#Ticket 384

Thanks, I’m sure there must be something wrong with the sql server. Because it was working very fine till years without any change.

1 Like

I strongly recommend using prepared statements as:

  1. They don’t break as often
  2. They are way more secure

Example:

$mysqli = new mysqli("localhost", "DBUSER", "DBPASS", "DBNAME");
$stmt = $mysqli -> prepare("INSERT INTO `db_name_users` (`username`, `password`) VALUES(?,?)");
$stmt -> bind_param("ss", $username, $password);
$stmt -> execute();
$stmt -> close();

As for why the query in itself isn’t working, it’s because its syntax is wrong. The syntax you’re using is for UPDATE. Insert goes like this:

INSERT INTO `table` (`row1`, `row2`) VALUES ('value1', 'value2');
2 Likes

Thanks for the reply. I wonder why this querymysql_query(“INSERT INTO db_name_users SET username = '”.$username."’, password = ‘".$password."’"); was working for more than 4 years if this is wrong.
Okay I can try the updated query. Btw what is ss in this statement

bind_param("ss", $username, $password);

S for string
I for integer
Etc…

@rajababar

As above, use prepared statements as suggested by ckhawand. Also this function is deprecated and works in php version <= 5.6. This may be also one of the reason for your query to not work properly.

Let us know if your issue is fixed or not.

1 Like

Thanks guys for the amazing support. Turns out the issue was with the sql query. In db I have two other columns (int). Previously they were automatically inserting 0 value by default. But now the sql was returning error complaining about missing column value. So I did that and also updated the apis form mysql to mysqli.

Regards

2 Likes