irqize
irqize

Reputation: 97

Can't insert value into mysql with php

I have this code, but it doesn't insert the value of $_SESSION['steamid'], as intended. Everything above the if statement appears to work, because if I put something else (a test line) inside the if statement, it executes.

$link = mysqli_connect($db_host, $db_user, $db_passwd, $db_dbase);
$query = mysqli_query($link, "SELECT * FROM users WHERE steamid='".$_SESSION['steamid']."'");
if (mysqli_num_rows($query) == 0) {
      mysqli_query($link, "INSERT INTO `users`(`steamid`) VALUES (".$_SESSION['steamid'].")");
}

Upvotes: 0

Views: 77

Answers (2)

Antoine Pointeau
Antoine Pointeau

Reputation: 399

You miss quote for the value, I think :

$link = mysqli_connect($db_host, $db_user, $db_passwd, $db_dbase); 
$query = mysqli_query($link, "SELECT * FROM users WHERE steamid='".$_SESSION['steamid']."'"); 
if (mysqli_num_rows($query) == 0) { 
    mysqli_query($link, "INSERT INTO `users`(`steamid`) VALUES ('".$_SESSION['steamid']."')"); 
}

Upvotes: 1

Nico
Nico

Reputation: 569

Your users table most likely contains many more REQUIRED fields then steamid.

Run your query manually on your db, the response will tell you that some fields are missing.

Just inserting the steamid like that doesn't really make sense. I'm guessing you'd want to do something like this.

$steamid = mysqli_real_escape_string($_SESSION['steamid']);
mysqli_query($link, 'INSERT INTO users (userid, steamid, <other required fields>) VALUES ("'.$userid.'", "'.$steamid.'", <other required fields>);"

Always escape your values, leaving things open for sql injection will create many bugs.

Upvotes: 0

Related Questions