Page 1 of 1

insert data in DB

Posted: Tue Aug 05, 2014 5:59 am
by terrenuit
Hi
I want to insert some datas in my DB, my scripts here run correctly, the server has no any error,
but my DB has nothing receive, can anyone help me ?

index.php

Code: Select all

<html>
<head>
<Title>Registration Form</Title>
<style type="text/css">
    body { background-color: #fff; border-top: solid 10px #000;
        color: #333; font-size: .85em; margin: 20; padding: 20;
        font-family: "Segoe UI", Verdana, Helvetica, Sans-Serif;
    }
    h1, h2, h3,{ color: #000; margin-bottom: 0; padding-bottom: 0; }
    h1 { font-size: 2em; }
    h2 { font-size: 1.75em; }
    h3 { font-size: 1.2em; }
    table { margin-top: 0.75em; }
    th { font-size: 1.2em; text-align: left; border: none; padding-left: 0; }
    td { padding: 0.25em 2em 0.25em 0em; border: 0 none; }
</style>
</head>
<body>
<h1>Register here!</h1>
<p>Fill in your name and email address, then click <strong>Submit</strong> to register.</p>
<form method="post" action="db.php" enctype="multipart/form-data" >
      Name  <input type="text" name="name" id="name"/></br>
      Email <input type="text" name="email" id="email"/></br>
      <input type="submit" name="submit" value="Submit" />
</form>
<?php

?>
</body>
</html>
db.php

Code: Select all

<?php
// DB connection info
$host = "localhost";
$user = "root";
$pwd = "";
$db = "testebase";
try{
    $conn = new PDO( "mysql:host=$host;dbname=$db", $user, $pwd);
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $sql = 'SELECT name,                                      
		          email
            FROM newbase';
    $conn->query($sql);
}
catch(Exception $e){
    die(print_r($e));
}
echo "<h3>register OK!.</h3>";
?>

Re: insert data in DB

Posted: Tue Aug 05, 2014 7:03 am
by Celauran
In db.php, you're selecting, not inserting. You also aren't capturing the results of the select. Try something like this:

Code: Select all

$query = "INSERT INTO newbase (name, email) VALUES (:name, :email)";
$stmt = $conn->prepare($query);
$exec = $stmt->execute([':name' => $_POST['name'], ':email' => $_POST['email']]);
You will also want to add some validation to the email field, but one thing at a time.

Re: insert data in DB

Posted: Tue Aug 05, 2014 1:56 pm
by terrenuit
Hi,
I did as you said, but the line: Parse error: syntax error, unexpected '[', expecting ')' in C:\wampserver32\www\karite1\db.php on line 13
$exec = $stmt->execute([':name' => $_POST['name'], ':email' => $_POST['email']]);

Code: Select all

<?php
// DB connection info
$host = "localhost";
$user = "root";
$pwd = "";
$db = "testebase";
try{
    $conn = new PDO( "mysql:host=$host;dbname=$db", $user, $pwd);
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
	
$query = "INSERT INTO newbase (name, email) VALUES (:name, :email)";
$stmt = $conn->prepare($query);
$exec = $stmt->execute([':name' => $_POST['name'], ':email' => $_POST['email']]);
	
echo "<h3>register OK!.</h3>";
?>


this line indicate: Parse error: syntax error, unexpected $end in C:\wampserver32\www\karite1\db.php on line 16
$exec = mysql_query($conn, $query) or die ("not OK");

Code: Select all

<?php
// DB connection info
$host = "localhost";
$user = "root";
$pwd = "";
$db = "testebase";
try{
    $conn = new PDO( "mysql:host=$host;dbname=$db", $user, $pwd);
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
	
$query = "INSERT INTO newbase (name, email) VALUES (:name, :email)";
$stmt = $conn->prepare($query);
$exec = mysql_query($conn, $query) or die ("not OK");
	
echo "<h3>register OK!.</h3>";
?>

Re: insert data in DB

Posted: Tue Aug 05, 2014 2:03 pm
by Celauran
You haven't closed your try block and your catch block is completely missing. Also, I used short array syntax in my example, which won't work on older versions (< 5.4) of PHP.

Re: insert data in DB

Posted: Tue Aug 05, 2014 2:05 pm
by Celauran
Updated to show the complete example, and made 5.3 compatible.

Code: Select all

<?php
// DB connection info
$host = "localhost";
$user = "root";
$pwd = "";
$db = "testebase";
try {
    $conn = new PDO( "mysql:host=$host;dbname=$db", $user, $pwd);
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

	$query = "INSERT INTO newbase (name, email) VALUES (:name, :email)";
	$stmt = $conn->prepare($query);
	$exec = $stmt->execute(array(':name' => $_POST['name'], ':email' => $_POST['email']));
}
catch(Exception $e) {
    die(print_r($e));
}
echo "<h3>register OK!.</h3>";
?>

Re: insert data in DB

Posted: Wed Aug 06, 2014 1:16 am
by terrenuit
@Celauran
it is OK, thank you so much