insert data in DB

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

insert data in DB

Post 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>";
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: insert data in DB

Post 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.
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

Re: insert data in DB

Post 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>";
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: insert data in DB

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: insert data in DB

Post 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>";
?>
terrenuit
Forum Commoner
Posts: 53
Joined: Tue Jul 08, 2014 2:18 pm

Re: insert data in DB

Post by terrenuit »

@Celauran
it is OK, thank you so much
Post Reply