我的INSERT INTO不能在PHP页面中工作,而是在SQL中工作

时间:2016-05-21 07:49:44

标签: php mysql

我这里有一个注册船员的页面,但我不知道什么似乎是问题。该查询在phpmyadmin中工作但在php页面中不起作用。 这是我的代码:

    session_start();
    require 'config.php';

    if (@$_SESSION['username']) {

    if (isset($_POST['first_name'])&&isset($_POST['middle_name'])&&isset($_POST['last_name'])&&isset($_POST['age'])&&isset($_POST['birth_date'])&&isset($_POST['birth_place'])&&isset($_POST['gender'])&&isset($_POST['martial_status'])&&isset($_POST['religion'])&&isset($_POST['nationality'])&&isset($_POST['email'])&&isset($_POST['address1'])&&isset($_POST['address2'])&&isset($_POST['course'])&&isset($_POST['school'])&&isset($_POST['remarks'])) {

        $first_name = $_POST['first_name'];
        $middle_name = $_POST['middle_name'];
        $last_name = $_POST['last_name'];
        $age = $_POST['age'];
        $birth_date = $_POST['birth_date'];
        $birth_place =$_POST['birth_place'];
        $gender = $_POST['gender'];
        $martial_status = $_POST['martial_status'];
        $religion = $_POST['religion'];
        $nationality = $_POST['nationality'];
        $email = $_POST['email'];
        $address1 = $_POST['address1'];
        $address2 = $_POST['address2'];
        $course = $_POST['course'];
        $school = $_POST['school'];
        $remarks = $_POST['remarks'];
        $date_added = date('Y-m-d');



            if (!empty($first_name)&&!empty($middle_name)&&!empty($last_name)&&!empty($age)&&!empty($birth_date)&&!empty($birth_place)&&!empty($gender)&&!empty($martial_status)&&!empty($religion)&&!empty($nationality)&&!empty($email)&&!empty($address1)&&!empty($course)&&!empty($school)) {


                    $query = "INSERT INTO `crew_info` (first_name,middle_name,last_name,age,birth_date,birth_place,gender,martial_status,religion,nationality,email_address,address_1,address_2,course,school_graduated,remarks,date_added,crew_status) VALUES ('$first_name','$middle_name','$last_name','$age','$birth_date','$birth_place','$gender','$martial_status','$religion','$nationality','$email','$address1','$address2','$course','$school','$remarks','$date_added','PENDING')";

                    echo 'Crew Successfuly Send to "PENDING PAGE"';

            }
            else {
                echo 'Some field is empty';
            }
    }


    echo '<!DOCTYPE html>
<html>
<head>
    <title>Add New Crew</title>
</head>
<body>

    <form action="add_crew.php" method="POST">
    <table>
        <tr>
            <td>
                First Name: 
            </td>
            <td>
                <input type="text" name="first_name" ></input>
            </td>
        </tr>
        <tr>
            <td>
                Middle Name: 
            </td>
            <td>
                <input type="text" name="middle_name" ></input>
            </td>
        </tr>
        <tr>
            <td>
                Last Name: 
            </td>
            <td>
                <input type="text" name="last_name" ></input>
            </td>
        </tr>
    </table><br>
    <table>
        <tr>
            <td>
                Age: 
            </td>
            <td>
                <input type="text" name="age" ></input>
            </td>
        </tr>
        <tr>
            <td>
                Birth Date: 
            </td>
            <td>
                <input type="text" name="birth_date" ></input>
            </td>
        </tr>
        <tr>
            <td>
                Birth Place: 
            </td>
            <td>
                <input type="text" name="birth_place" ></input>
            </td>
        </tr>
    </table><br>
    <table>
        <tr>
            <td>
                Gender: 
            </td>
            <td>
                <input type="text" name="gender" ></input>
            </td>
        </tr>
        <tr>
            <td>
                Martial Status: 
            </td>
            <td>
                <input type="text" name="martial_status" ></input>
            </td>
        </tr>
        <tr>
            <td>
                Religion: 
            </td>
            <td>
                <input type="text" name="religion" ></input>
            </td>
        </tr>
    </table><br>
    <table>
        <tr>
            <td>
                Nationality: 
            </td>
            <td>
                <input type="text" name="nationality" ></input>
            </td>
        </tr>
        <tr>
            <td>
                Email Address: 
            </td>
            <td>
                <input type="text" name="email" ></input>
            </td>
        </tr>
    </table><br>
    <table>
        <tr>
            <td>
                Address 1:
            </td>
            <td> 
                <input type="text" name="address1" ></input>
            </td>
        </tr>
        <tr>
            <td>
                Address 2: 
            </td>
            <td>
                <input type="text" name="address2"></input>
            </td>
        </tr>
    </table><br>
    <table>
        <tr>
            <td>
                Course: 
            </td>
            <td>
                <input type="text" name="course" ></input>
            </td>
        </tr>
        <tr>
            <td>
                School Graduated: 
            </td>
            <td>
                <input type="text" name="school" ></input>
            </td>
        </tr>
    </table><br>
    <table>
        <tr>
            <td>
                Remarks: 
            </td>
            <td>
                <input type="text" name="remarks"></input>
            </td>
            </tr>
        </table><br>
            <input type="submit" value="Submit"></input>
        </form>


    </body>
    </html>';

        }
        else {  header('Location: /practice1/index.php');
        }



        ?>

这是php的整个页面

6 个答案:

答案 0 :(得分:0)

就像萨蒂说你忘记插入查询一样

$conn->query($query); // PDO for new php7



mysql_query($query,$conn); // for old code but this is deprecated

//$conn is mysql_connect in config.php (it's may be in another variable up on your write)

答案 1 :(得分:0)

<?php if (!empty($first_name)&&!empty($middle_name)&&!empty($last_name)&&!empty($age)&&!empty($birth_date)&&!empty($birth_place)&&!empty($gender)&&!empty($martial_status)&&!empty($religion)&&!empty($nationality)&&!empty($email)&&!empty($address1)&&!empty($course)&&!empty($school)) {   

 $query = "INSERT INTO `crew_info` (first_name,middle_name,last_name,age,birth_date,birth_place,gender,martial_status,religion,nationality,email_address,address_1,address_2,course,school_graduated,remarks,date_added,crew_status)
VALUES ('$first_name','$middle_name','$last_name','$age','$birth_date','$birth_place','$gender','$martial_status','$religion','$nationality','$email','$address1','$address2','$course','$school','$remarks','$date_added','PENDING')";


 // for mysqli code starts

 mysqli_query($con, $query);

 // for mysqli code ends  where $con is connection variable

                        echo 'Crew Successfuly Send to "PENDING PAGE"';

                }
?>

你必须触发查询才能插入数据库,我想你忘记了

答案 2 :(得分:0)

在查询后添加此代码以执行它

                    if (mysqli_query($conn,$query)) {
                        echo 'Your request is sent to queue';
                    }
                    else {
                        echo 'Something went wrong';
                    }

答案 3 :(得分:0)

我认为你错过了两分    1.你的php和mysql之间的连接。    2.解雇查询。

尝试使用此代码段。

SELECT C.Nom, E.Caution, E.Kilometrage 
FROM FACTURATION AS F 
LEFT JOIN ENREGISTREMENT AS E ON F.Enregistrement=E.Numero 
LEFT JOIN RESTITUTION AS R ON F.Restitution=R.Numero 
LEFT JOIN RESERVATION AS X ON F.Numero=X.Facturation 
LEFT JOIN CLIENT AS C ON X.Numero_Client=C.Numero

//与数据库的连接

$username = "your_name";
$password = "your_password";
$dbhost = "localhost"; 

$conn = mysql_connect($dbhost, $username, $password);

然后把

if(! $conn )
   {
     die('Could not connect: ' . mysql_error());
   }

   echo 'Connected successfully';

之后解雇您的查询

 if (!empty($first_name)&&!empty($middle_name)&&!empty($last_name)&&!empty($age)&&!empty($birth_date)&&!empty($birth_place)&&!empty($gender)&&!empty($martial_status)&&!empty($religion)&&!empty($nationality)&&!empty($email)&&!empty($address1)&&!empty($course)&&!empty($school)) {


                    $query = "INSERT INTO `crew_info` (first_name,middle_name,last_name,age,birth_date,birth_place,gender,martial_status,religion,nationality,email_address,address_1,address_2,course,school_graduated,remarks,date_added,crew_status) VALUES ('$first_name','$middle_name','$last_name','$age','$birth_date','$birth_place','$gender','$martial_status','$religion','$nationality','$email','$address1','$address2','$course','$school','$remarks','$date_added','PENDING')";

使用后关闭连接
if (mysqli_query($conn,$query)) 
     {
      echo 'Crew Successfuly Send to "PENDING PAGE"';
     }
  else 
      {
       echo 'Some Syntax is wrong';
      }

            }
            else {
                echo 'Some field is empty';
            }

答案 4 :(得分:0)

上面的

@prakash是最接近正确的... 但是,我认为,使用value =“”将您所有的输入都设置在html上,这样您就不必执行所有的if(!empty()垃圾操作,以后再获取空白...

$username = "your_name";
$password = "your_password";
$dbhost = "localhost";
$dbname = "your_db";

// just good practice to specify the db, later you may have multiple on the same server..

// use mysqli as someone else above stated...
$conn = mysqli_connect($dbhost, $username, $password,$dbname);


if(!$conn){
    die('Connect Error: ' . mysqli_connect_error()); 
    //again.. mysqli.. not mysql
}

// dont kill yourself with manual entry errors for the query... 
//  move $_POST to a new variable and prep the array for a loop;
$data = $_POST;

// get rid of the submit variable
unset($data['submit']); 

// assuming this db field is int not varchar
// and assuming birthdate field is DATE or varchar
$data['age'] = intval($data['age']); 
$data['birthdate'] = date('Y-m-d',strtotime($date['birthdate'])); 


// would be easier to set default value NOW() for date_added column and DATETIME
// NOW() will set that field to the CURRENT_TIMESTAMP on every insert for you...
// but the way you have it, and assuming db field is DATE or varchar
$data['date_added'] = date('Y-m-d');

 // Same for 'crew_status' .. field VARCHAR or ENUM...  default value 'PENDING'
$data['crew_status'] = 'PENDING';

//  make a string variable to fill with fields...
$fields = '';

// make a string variable to to fill with insert values....
$values = '';

//  loop through $data, add each value followed by a comma to string 
// no single quotes on integers where the db field is INT .... like age...
foreach($data as $key=>$val){

    //  this is looking for the value="" from when a user blanks an input field....
    if($val == ''){

        // handles blank user inputs.. db fields NOT set 'not null'..no quotes around NULL
        $values .= 'NULL,';  

    // if the value is not blanked... look for strings and dates....
    }elseif(!is_int($val)){

       // real_escape_string will kill any characters that will error like ' or \ 
       // and remove in sql injection risks .. single quotes on varchar db field vals
       $values .="'".mysqli_real_escape_string($conn,$val)."',";

    // if input is not blank and !is_int() must be INT...age
    }else{

       //no quotes on integers going into INT db fields....
       $values .= $val.',';
    }

    // add the $key (field name) to the $fields same order as $values, comma after each
    // backticks around field names...
    $fields .=  "`".$key."`,"; 

}

// we are dragging an extra comma on $fields and $values at the end of each string
// we will get them in the insert query string....splitting up the query to explain

$query = "INSERT INTO `crew_info`";

//substr off the comma $fields is dragging "," put it between ( and )
$query .= "(".substr($fields,0,-1).")"; 

//substr off the comma $values dragging "," put it between ( and )
$query .= " VALUES (".substr($values,0,-1).")";   


//not split query might be confusing to read..query would look like...
//"INSERT INTO `crew_info`(".substr($fields,0,-1).") VALUES (".substr($values,0,-1).")"


//now insert 
if(!mysqli_query($conn,$query)){

     // if there is an error... let someone know!
     die('ooops!...'.mysqli_error());

}else{
     // if no error......
     echo 'you just inserted data!!! id# = '. mysqli_insert_id($conn);
}

答案 5 :(得分:-1)

您的sql字符串中有错误。 INSERT INTO表 VALUES (..,..);

$query = "INSERT INTO `crew_info` VALUES (first_name,middle_name,last_name,age,birth_date,birth_place,gender,martial_status,religion,nationality,email_address,address_1,address_2,course,school_graduated,remarks,date_added,crew_status) VALUES ('$first_name','$middle_name','$last_name','$age','$birth_date','$birth_place','$gender','$martial_status','$religion','$nationality','$email','$address1','$address2','$course','$school','$remarks','$date_added','PENDING')";