Search code examples
phpmysqlmysql-error-1064database-table

PHP insert error 1064


I keep getting an error on my php page. When I submit my form it says I don't have authorisation to see the php page and when I put it into sql query test it says it's error #1064. I keep getting this error message and I have no idea why. In the query test I'm only putting the info between . Can someone please help fix this?

<html>
<head>
<meta charset="utf-8">
<title>Enquiry</title>
</head>

<body> 
<?php

$name=$_POST['name'];
$email=$_POST['email'];
$phone = $_POST['phone'];
$radio = $_POST['radio'];
$enquiry = $_POST['enquiry'];

$user="root";
$password="";
$database="test";

mysql_connect('localhost',$user,$password) or die("Unable to connect to server"); 
mysql_select_db($database) or die("Unable to select database"); 
$query = "INSERT INTO Enquiry VALUES ('','$name','$email','$phone','$radio','$enquiry')";
mysql_query($query); 
mysql_close(); 

?>
</body>
</html>

Solution

  • <?php
    $con= mysql_connect("localhost","root","") or die('Unable to connect to server:'mysql_error());
    mysql_select_db("test",$con);
    if(isset($_POST['submit']))  
    {
    
    $name=test_input($_POST['name']);
    $email=test_input($_POST['email']);
    $phone = test_input($_POST['phone']);
    $radio = test_input($_POST['radio']);
    $enquiry = test_input($_POST['enquiry']);
    $sql = "
    "INSERT INTO `Enquiry`"." (
        name,
        email,
        phone,
        radio,
        enquiry
    )". "VALUES". "(
        "$name",
        "$email",
        "$phone",
        "$radio",
        "$enquiry"
    )
    ";
     $res=mysql_query($sql);
    
    if(!$res){
    die('Could not enter data: ' . mysql_error());
    }
    echo 'your successfull msg goes here';
    
    function test_input($data)
    {
    $data = trim($data);
    $data = stripslashes($data);
    $data = mysql_real_escape_string($data);
    return $data;
    }
    
    
    ?>
    

    assuming that primaryid is auto increment as said . try this block of code