php 不会插入到 mysql 中。没有错误

标签 php mysql

我创建了 php 和 mysql 数据库和表。这是我的代码:

<form action="proba.php" method="post" />
	<p> ime: <input type="text" name="ime" /></p>
    <p> prezime: <input type="text" name="prezime" /></p>
    <p> datum rodjenja: <input type="text" name="godiste" /></p>
    <p> jmbg: <input type="text" name="jmbg" /></p>
    <p> adresa: <input type="text" name="adresa" /></p>
    <p> email: <input type="text" name="email" /></p>
    <p> telefon: <input type="text" name="telefon" /></p>
    <p> datum: <input type="text" name="datum" /></p>
    
    <input 	type="submit" value="insert" />
</form>

这是我连接mysql的代码

<?php


$link = mysqli_connect("127.0.0.1", "root", "1511", "test");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;

$db_selected = mysql_select_db ('test', $link);

if (!$db_selected) {
	die('nedostupno ' . test . ' : ' . mysql_error ());
	
}

$values = $_POST['ime'];
$values2 = $_POST['prezime'];
$values3 = $_POST['godiste'];
$values4 = $_POST['jmbg'];
$values5 = $_POST['adresa'];
$values6 = $_POST['email'];
$values7 = $_POST['telefon'];
$values8 = $_POST['datum'];


$sql = "INSERT INTO users (ime, prezime, godiste, jmbg, adresa, emal, telefon, datum) VALUES ('values', 'values2', 'values3', 'values4', 'values5', 'values6', 'values7', 'values8')";

}
echo 'Connected successfully';

?>

这是 mysql:

mysql

最佳答案

您犯了一些错误,因此查询可能不会将数据插入到 phpmyadmin 数据库中。您犯的基本错误是在插入查询中没有连接您在 VALUES 部分中需要的值,插入语句语法将如下所示。

插入语法:

INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

Note: If a column is AUTO_INCREMENT (like the "id" column) or TIMESTAMP (like the "reg_date" column), it is no need to be specified in the SQL query; MySQL will automatically add the value.

因此基本形式将与您在问题中显示的相同。我在提交按钮上单独添加了一个名称并重新修改。

HTML 格式:

<form action="proba.php" method="post" />
    <p> ime: <input type="text" name="ime" /></p>
    <p> prezime: <input type="text" name="prezime" /></p>
    <p> datum rodjenja: <input type="text" name="godiste" /></p>
    <p> jmbg: <input type="text" name="jmbg" /></p>
    <p> adresa: <input type="text" name="adresa" /></p>
    <p> email: <input type="text" name="email" /></p>
    <p> telefon: <input type="text" name="telefon" /></p>
    <p> datum: <input type="text" name="datum" /></p>    
    <input type="submit" name="save" value="insert" />
</form>

您的 proba.php 将如下所示,因为我在下面进行了编码。

<?php
//Database connection Part of Mysqli
$host="localhost";
$user="root";
$password="1511";
$db="test";
$conn=new mysqli($host,$user,$pass,$db);
// Print Error if the connection is failed.
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
// Print Error if the DB is not selected.
if (!mysqli_select_db($conn, $db)) {
    die("Uh oh, couldn't select database --> $db" . $conn->connect_error . ' >');
}
if(isset($_POST['save']))
{
    $values = $_POST['ime'];
    $values2 = $_POST['prezime'];
    $values3 = $_POST['godiste'];
    $values4 = $_POST['jmbg'];
    $values5 = $_POST['adresa'];
    $values6 = $_POST['email'];
    $values7 = $_POST['telefon'];
    $values8 = $_POST['datum'];
    $sql = "INSERT INTO users (`ime`, `prezime`, `godiste`, `jmbg`, `adresa`, `emal`, `telefon`, `datum`) VALUES ('".$values."', '".$values2."', '".$values3."', '".$values4."', '".$values5."', '".$values6."', '".$values7."', '".$values8."')";
    $query = mysqli_query($conn,$sql);
    echo 'Inserted successfully';
}
?>

Note: You first put echo to the Insert Statement and then break the execution by putting the exit; and you copy the statement that is echoed and place it in SQL of the DB and then check whether any error occurs in insertion. If no error occurs remove the echo and delete the exit;

并且您已成功插入数据。希望我能就未插入数据库的数据给出明确的解释。

关于php 不会插入到 mysql 中。没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39527440/

相关文章:

php - Symfony 3 Doctrine 未定义索引

java - 如何设置 Hibernate 配置属性以使用 Fabric 驱动程序连接到 Mysql

php - 从表单设置 session 变量

PHP mysql连接?

php - 如何计算除列的重复条目之外的所有行?

mysql - MySQL中如何选择记录类型

php - 使用tank auth时数据库连接突然增加

php - Yii2:为模型的所有字段设置默认值

php - mod_rewrite 不适用于 jQuery.ajax

javascript - 无法读取未定义的 JQuery 选项标记的属性