简体   繁体   English

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

[英]php won't insert into mysql. no errors

I create php and mysql database and table.我创建了 php 和 mysql 数据库和表。 This is my code:这是我的代码:

 <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>

and here is my code to connect with mysql这是我与 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'; ?>

And this is mysql:这是mysql:

mysql

You have made some few mistakes so that the query might not be inserting datas into the phpmyadmin database.您犯了一些错误,因此查询可能没有将数据插入到 phpmyadmin 数据库中。 The basic error you have made is in the insert query by not concatenating the values that you want in the VALUES section and the insert statement syntax will be like this.您犯的基本错误是在插入查询中没有连接您想要在VALUES部分中的VALUES ,插入语句的语法将是这样的。

Insert Syntax:插入语法:

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;注意:如果某列是AUTO_INCREMENT(如“id”列)或TIMESTAMP(如“reg_date”列),则无需在SQL查询中指定; MySQL will automatically add the value. MySQL 会自动添加该值。

So the basic form will be the same as you display in the question.因此,基本形式将与您在问题中显示的相同。 I have added a name alone to the submit button and re-modified it.我在提交按钮上单独添加了一个名称并重新修改了它。

HTML FORM: 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>

And your proba.php will look like this as i have coded below.你的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;注意:您首先将echo放入 Insert 语句,然后通过放入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.复制回显的语句,放到数据库的SQL中,然后检查插入是否有错误。 If no error occurs remove the echo and delete the exit;如果没有出现错误,删除echo并删除exit;

And you are inserting the data successfully.并且您正在成功插入数据。 Hope so i would have given a clear explanation about the data not inserting into the database.希望所以我会对未插入数据库的数据给出明确的解释。

Do something like this:做这样的事情:

$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."')";
mysqli_query($link,$sql);

From the very first mistake.从第一个错误开始。

  • You have added your success code in if condition where Server connection object is gets fail to connect.您已在服务器连接对象无法连接的情况下添加了成功代码。
  • When you are using mysqli_connect for server connection then why you are using mysql_connect.当您使用 mysqli_connect 进行服务器连接时,为什么要使用 mysql_connect。
  • Missing of query execution line.缺少查询执行行。 ie mysqli_query($link, $sql).即 mysqli_query($link, $sql)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM