i'm trying to do the following but i have this error
it doesn't store in the my DB
DB name = job , table login , attributes : loginId , loginname , loginpassword
<html>
<head>
<?php
error_reporting(E_ALL);
?>
<?php
$link = mysql_connect('localhost', 'root', '123456');
echo "from DB";
mysql_select_db('job', $link);
?>
<?
class user {
private $userId;
private $username;
private $password;
public function getUsername() {
return $this->username;
}
public function setUsername($username) {
$this->username = $username;
}
public function getUserId() {
return $this->userId;
}
public function setUserId($userId) {
$this->userId = $userId;
}
public function getPassword() {
return $this->password;
}
public function setPassword($password) {
$this->password = $password;
}
function __construct() {
}
}
?>
</head>
<body>
<form action="" method="POST" >
username :<input type="text" size="30" name="username" >
password: <input type="password" size="30" name="password">
<input type="submit" value="singup!" name="submit" />
</form>
<?php
function addNewuser($user, $link) {
global $userModel;
echo "$user->getUsername()";
$query = sprintf("INSERT INTO login ('loginname','loginpassword') values('%s' ,'%s')",
$user->getUsername(),
$user->getPassword()
);
$generatedId = mysql_insert_id($link);
mysql_query($query, $link);
echo "AFTER ADD FUNCTION";
$query2 = sprintf("SELECT loginname FROM login WHERE loginname='%s' AND loginpassword='%s'",
$user->getUsername(),
$user->getPassword());
$result = mysql_query($query2, $link);
echo "$result";
while ($row = mysql_fetch_array($result)) {
echo $row['loginname'];
}
}
if (isset($_POST['submit'])) {
$user = New User();
$username = $_POST['username'];
$password = $_POST['password'];
$user->setUsername($username);
$user->setPassword($password);
addNewuser($user, $link);
echo "add done ! ";
mysql_close($link);
}
?>
</body>
Your insert query syntax is wrong. Should be:
$query = sprintf("INSERT INTO login (loginname,loginpassword) values('%s' ,'%s')",
$user->getUsername(),
$user->getPassword()
);
Also please read about SQL injection as your code is vulnerable.
According to your problem, which is
DB name = job , table login , attributes : loginId , loginname , loginpassword
I think your code at this bit
$query = sprintf("INSERT INTO login ('loginname','loginpassword') values('%s' ,'%s')",
$user->getUsername(),
$user->getPassword()
);
is missing loginID attribute, you can change it to
$query = sprintf("INSERT INTO login (loginId, loginname, loginpassword) values('','%s' ,'%s')",
$user->getUsername(),
$user->getPassword()
);
It should work. And please remember to eliminate single quotes in your query string :)
PHP is case sensitive.
Fix it to:
if (isset($_POST['submit'])) {
$user = new user();
...
Also make sure that PHP erros are on when you are checking the code, because your current implementation should have thrown an error at that line
精彩评论