0

Hi I am doing this HTML.net tutorial on php.I keep getting this error and I cannot figure out how to fix it.I am running php version 5.4 maybe this is the problem, I appreciate tour help:
Parse error: syntax error, unexpected 'TABLE' (T_STRING) in C:\inetpub\wwwroot\phpconnect.php on line 6
This is the code:

<?php
mysql_connect("localhost", "root", "LaTorre1937##") or die(mysql_error());
    mysql_select_db("mydatabase") or die(mysql_error());

    CREATE TABLE people (
    id INT NOT NULL AUTO_INCREMENT,
    FirstName CHAR, 
    LastName CHAR,
    Phone INT,
    BirthDate DATE,
    PRIMARY KEY(id)
    );

?>
5
Contributors
8
Replies
38
Views
4 Years
Discussion Span
Last Post by TonyG_cyprus
0

The create table needs to be in a string, to be executed by the mysql_query function. It's an SQL script, not PHP code.

0

Hi, I am sorry I posted the wrong code.This happened because I am doing 3 different tutorials at the same time so I guess I got confused, I know the difference between sql language and php.The code below is the one I wanted to ask for some help, I keep getting this errors:
Notice: Undefined index: firstname in C:\inetpub\wwwroot\datain.php on line 14

Notice: Undefined index: lastname in C:\inetpub\wwwroot\datain.php on line 15

Notice: Undefined variable: name in C:\inetpub\wwwroot\datain.php on line 21
Thanks for your help
ERROR

<HTML>
<BODY>
<form method="post" action="datain.php">
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Nick Name:<input type="Text" name="nickname"><br>
E-mail:<input type="Text" name="email"><br>
Salary:<input type="Text" name="salary"><br>
<input type="Submit" name="submit" value="Enter info">
</form>
</HTML>

<?php

 $host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="LaTorre1937##"; // Mysql password 
$db_name="learndb"; // Database name 
$tbl_name="personnel"; // Table name 

// Connect to server and select database.
 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
 $firstname=$_POST['firstname'];
 $lastname=$_POST['lastname'];
 $nickname=$_POST['nickname'];
 $email=$_POST['email'];
 $salary=$_POST['salary'];

 // Insert data into mysql 
 $sql="INSERT INTO $tbl_name(name, lastname, email)VALUES('$name', '$lastname', '$email')";
 $result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
 if($result){
 echo "Successful";
 echo "<BR>";
 echo "<a href='insert.php'>Back to main page</a>";
 }

 else {
 echo "ERROR";
 }
 ?> 

 <?php 
// close connection 
 mysql_close();
 ?>
0

Hi thanks for your help.I am not sure though where to insert that isset code.If I am not asking too much as I am a beginner could you please amend that code for me so I can figure it out? If not thanks a lot anyway I'll keep studying and trying.

0
if (isset($_POST['submit']))
    {
    // form submited so prosses the results
    // and put them into the db
    // tell the user succes or fail 
    }
else
    {
    // form not submited so lets display it
    ?>
    <form ...
    </form>
    <?
    }
1

It won't work, because you have errors.

First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>

is in your form markup but,

// Get values from form 
 $firstname=$_POST['firstname'];
 $lastname=$_POST['lastname'];

is what your code is looking for. The 'name' attribute in the form must match the 'S_POST' request.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.