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

6
  • 3
    you didn't executed your code . $result = mysqli_query($link,$sql); Commented Sep 16, 2016 at 8:59
  • 1
    You're also mixing mysql_* and mysqli_* functions, which you can't do. Commented Sep 16, 2016 at 9:00
  • 1
    mixture of mysql and mysqli code + query didn't fired .. Commented Sep 16, 2016 at 9: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. Commented Sep 16, 2016 at 9: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 prnt.sc/cimy2l Commented Sep 16, 2016 at 10:18

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

15 Comments

i tried with with your code and got this prntscr.com/cir589 now i will kill myself :D
Sorriee bro. Change this line to $conn=new mysqli($host,$user,$pass,$db); as i give below
$conn=new mysqli($host,$user,$password,$db);
and try now this is the error and we shall crack through
@SandiBudic. Sorriee for mistake bro :)
|
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);

1 Comment

i tried it but no changes.. i will delete all and start from begening. thank you
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).

4 Comments

i was tried to use mysql_connect but i got this error 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
Did your issue with inserting records in database fixed!?
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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.