LATER EDIT: It woorks to insert a new row, but ONLY one! After inserting one user to my Users table, I cannot insert others :D it (again) keeps alerting "Could not enter data". Why?
I already have a database named "problema_curs_1" and I want to insert a new row into my Users table. This is the code:
$db['user'] = 'root';
$db['pass'] = '';
$db['server'] = 'localhost';
$conn = mysql_connect($db['server'], $db['user'], $db['pass']);
if(! $conn )
{
die(json_encode(array("mesaj" => "'Could not connect: ")));
}
$sql = "INSERT INTO Users ('nume', 'password', 'rol') VALUES($numereg,$parolareg,'user')";
mysql_select_db('problema_curs_1');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die(json_encode(array("mesaj" => "Could not enter data:")));
}
echo "Entered data successfully\n";
mysql_close($conn);
It pops up "Could not enter data:". My table has 4 columns: ID, "nume", "password" and "rol". ID is set to autoincrement, and this is why I don't want to insert it manually.
*This I will use in my register() function. *If it helps, I can put here the way I wrote the login() part, which seems to work:
function login($nume,$parola)
{
$db['user'] = 'root';
$db['pass'] = '';
$db['server'] = 'localhost';
$conn = mysql_connect($db['server'], $db['user'], $db['pass']);
$sql = "SELECT id FROM `Users` WHERE nume ='".$nume."' ANY password = '".md5($parola)."'";
$q = mysql_query($sql);
if(!$q)
die(json_encode(array("mesaj" => "Invalid")));
else{
$x = mysql_fetch_array($q);
if (empty($x))
die(json_encode(array('mesaj' => 'User does not exist')));
else {
// $user = new User($x['id']);
session_start();
$_SESSION['user_id'] = $x['id'];
$_SESSION['loggedin'] = "yes";
die(json_encode(array("mesaj"=>"Congrats :)")));
}
}
//nu prea are cum sa ajunga aici
die(json_encode(array("mesaj"=>"You were not logged")));
}