PHPを学び始めたばかりなので、間違ったことを理解できず、mysqlのデータベースにデータが登録されません。私はxamppを使用しています。すべての名前とデータは正しく書かれています。私はすべての機能をチェックしました...まだ動作しません。mysqlのデータをPHPで保存する方法
<DOCTYPE html>
<html>
<head>
<title>New Account</title>
</head>
<body>
<?php
/*
Implementare:
-functie de verificare daca functia exista in baza de date
-function bool_email($POST_('email adress'))
-functie comparare stringuri
*/
$name = '';
$password = '';
$gender = '';
$email = '';
$tc = '';
if(isset($_POST['submit']))
{
$ok = true;
if(!isset($_POST['name']) || $_POST['name'] === '' /*bool_name_exist($the string)*/)
{
$ok = false;
printf("You forget about name or it exist already in the database!");
echo '<br>';
}
else
{
$name = $_POST['name'];
}
if(!isset($_POST['password']) || $_POST['password'] === '')
{
$ok = false;
printf('You forget about password!');
echo '<br>';
}
else
{
$password = $_POST['password'];
}
if(!isset($_POST['password_conf']) || $_POST['password_conf'] === '' || strcmp($_POST['password'], $_POST['password_conf']) != 0)
{
$ok = false;
printf('You forget about password or password conf. does not match!');
echo '<br>';
}
if(!isset($_POST['email']) || $_POST['email'] === '' /*functie care verifica daca emailul e valid sau este in baza de date*/)
{
$ok = false;
printf('You forget about email or your email address is not validate!');
echo '<br>';
}
else
{
$email = $_POST['email'];
}
if(!isset($_POST['gender']) || $_POST['gender'] === '')
{
$ok = false;
printf('You forget about gender!');
echo '<br>';
}
else
{
$gender = $_POST['gender'];
}
if(!isset($_POST['tc']) || $_POST['tc'] === '')
{
$ok = false;
printf('You need to validate t&c!');
echo '<br>';
}
else
{
$tc = $_POST['tc'];
}
if($ok === true)
{
printf('your account has been registered');
}
if($ok)
{
$db = mysqli_connect('localhost', 'root', '', 'platformaworkout');
$sql = sprintf("INSERT INTO registerdata (Name, Gender, Password, Email), VALUES ('%s', '%s', '%s', '%s') ",
mysqli_real_escape_string($db, $name),
mysqli_real_escape_string($db, $gender),
mysqli_real_escape_string($db, $password),
mysqli_real_escape_string($db, $email));
printf('<br>%s', $sql);
if(mysqli_query($db, $sql))
{
echo '<br>it work';
}else
{
echo '<br>I want to kill myself...';
}
mysqli_close($db);
echo '<p>user added</p>';
}
}
?>
<form method="post" action="">
<header>Register new account:<br></header>
UserName:<input type="text" name="name" value="<?php echo htmlspecialchars($name); ?>"><br>
Password:<input type="password" name="password" value="<?php echo htmlspecialchars($password); ?>"><br>
Password confirmation:<input type="password" name="password_conf"><br>
Gender:
<input type="radio" name="gender" value="f" <?php if($gender === 'f'){echo 'checked';}?>>female
<input type="radio" name="gender" value="m" <?php if($gender === 'm'){echo 'checked';}?>>male<br>
Email Address:<input type="text" name="email" value="<?php echo htmlspecialchars($email); ?>"><br>
<input type="checkbox" name="tc" value="ok" <?php if($tc === 'okay'){echo 'checked';}?>>I accept t&c!<br>
<a src="#">Read t&c!</a>
<input type="submit" name="submit" value = "submit">
</form>
</body>
</html>
What I see in the browser My database structure
「ユーザーが追加されました」というメッセージが表示されますか? (私はあなたの変数をバインドするのを忘れたと思います) –
最初からパラメータを使用する方法を学んでください。物事をもっと楽にするでしょう。 –
私はメッセージを見た "user added"、またprintf変数を試してみました。 – Jarlio