2

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

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
Sandi Budic
  • 59
  • 2
  • 10
  • 3
    you didn't executed your code . $result = mysqli_query($link,$sql); – Alive to die - Anant Sep 16 '16 at 08:59
  • 1
    You're also mixing mysql_* and mysqli_* functions, which you can't do. – Jonnix Sep 16 '16 at 09:00
  • 1
    mixture of mysql and mysqli code + query didn't fired .. – Vivek Singh Sep 16 '16 at 09:00
  • You added all code in wrong `if` condition. You added everything in condition where `!$link` . Your code means that If your mysqli connection is not successful, then only do all the required tasks of insertion. Thats not possible and is wrong. Also you didn't execute your sql query. – jarvo69 Sep 16 '16 at 09:03
  • ok, thank you all. i will delete it and start from begining. i will learn more about mysqli because when put mysqli this erroe is gone http://prnt.sc/cimy2l – Sandi Budic Sep 16 '16 at 10:18
  • You might also want to take a read of [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-ph) as your code is vulnerable. – Dezza Sep 16 '16 at 12:41

3 Answers3

1

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.

Naresh Kumar P
  • 4,127
  • 2
  • 16
  • 33
0

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);
Virb
  • 1,639
  • 1
  • 16
  • 25
0

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. i.e. mysqli_query($link, $sql).
Keyur Mistry
  • 926
  • 6
  • 18
  • i was tried to use mysql_connect but i got this error http://prntscr.com/cimy2l then i put mysqli_connect and error is gone... im very new in this and i dont know differences too much about mysql and msqli. it is still chinese language for me but im learning. :D – Sandi Budic Sep 16 '16 at 10:15
  • Did your issue with inserting records in database fixed!? – Keyur Mistry Sep 16 '16 at 10:18
  • im tring something new with mysqli only and some other codes but still nothing. here is my new codehttp://prntscr.com/cinite im little woried... im that much stupid :D – Sandi Budic Sep 16 '16 at 11:11
  • If you are still getting white page and not see the inserted records then please try to get mysqli query errors. it will definatly shows you an error. Do little R&D for mysqli query erros to print – Keyur Mistry Sep 16 '16 at 11:25