简体   繁体   中英

php won't insert into mysql. no errors

I create php and mysql database and table. 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

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

You have made some few mistakes so that the query might not be inserting datas into the phpmyadmin database. 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.

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; MySQL will automatically add the value.

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:

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

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

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.
  • Missing of query execution line. ie mysqli_query($link, $sql).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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