Jump to content

Recommended Posts

Hi,

 

First of all, I'd like to say I'm new to PHP but I'm enjoying it a lot  :) I've been doing it at uni so far but tried to set it up on my web server at home. I have a working install of PHP and I can connect to my database using a DSN (called db09) which allows me to read data from a very simple table in the database.

 

However, the problems arise when I try to use INSERT INTO to insert data into the database from a form. Here is my code :

 

--- THIS WORKS ---

<html>

<head>

<title>PHP Database 1</title>

</head>

<body>

<H1>PHP Example: Select all data from a database</H1>

<?php

$DSN="db09";

$conn= odbc_connect('db09','','');

$sql="Select * from tblCustomer";

$rs=odbc_exec($conn,$sql);

echo ("<p>Customers details:");

echo ("<table border='1'>");

While (odbc_fetch_row($rs)) {

$Surname=odbc_result($rs,"Surname");

 

 

echo ("<tr><td>Name:</td><td>$Surname</td>");

 

}

odbc_close($conn);

?>

 

</body>

</html>

 

-----------------------

THIS DOESN'T WORK - FORM.

-------------------------

 

<html>

<head>

</head>

<body>

<p>Input a new customer record</p>

 

<form method="post" action="register.php">

 

<p>Surname: <input type="text" name="surname" /> </p>

<p>Forename: <input type="text" name="forename" /></p>

<p>DOB: <input type="text" name="dob" /> </p>

<p>Email: <input type="text" name="email" /> </p>

 

 

<input type="submit">

 

</form>

 

</body>

</html>

 

---------------------

AND HERE IS THE PHP

-------------------

<html>

<head>

</head>

 

<body>

<?php

 

$surname = $_POST ['surname'];

$forename = $_POST ['forename'];

$dob = $_POST ['dob'];

$email = $_POST ['email'];

 

$conn=odbc_connect('db09','','');

 

$sql="INSERT INTO "tblCustomer" ("Surname", "Forename", "DOB", "Email Address") VALUES ($surname','$forename', '$dob','$email')";

 

if (odbc_exec($conn,$sql)) {

echo "Record added";

}

 

odbc_close($conn);

 

?>

 

</body>

-------------------

 

And the table name and the field names are right ... I checked them lots ;)

 

any help would be much appriciated  :)

 

Thank you :)

 

David Harrison

 

 

 

 

 

 

In general you have mixed up the quotes.

See it colored

$sql="INSERT INTO "tblCustomer" ("Surname", "Forename", "DOB", "Email Address") VALUES ($surname','$forename', '$dob','$email')";

 

(use


or


tags when posting your code in the forum.

 

Apart from that Access needs [] around column names with spaces.

  • 2 weeks later...

Nevermind, I've got it working now too;

 

<?php

$name = $_POST ['name'];
$email = $_POST ['email'];

$conn=odbc_connect('calrim','','');

$sql="INSERT INTO CONTACTS (name,email) VALUES ('$name', '$email')";

if (odbc_exec($conn,$sql)) {
   echo "Record added";
   }

odbc_close($conn);

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.