2014-01-09 33 views
1

我在html中設計了一個註冊表單。並嘗試使用此PHP代碼在MySQL數據庫中進行輸入。但我不能在數據庫中添加記錄。 php文件的代碼如下所示:無法在mysql數據庫中使用php代碼添加記錄

注:該數據庫名是rku_database_main

<?php 
     $con = mysqli_connect("localhost","root","","rku_database_main"); 
     if (mysqli_connect_errno()) 
     { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
     } 
       $us_student_id= $_POST['student_id']; 
     $us_user_id= $_POST['user_id']; 
     $us_name= $_POST['name']; 
     $us_permanent_address= $_POST['permanent_address']; 
     $us_local_address= $_POST['local_address']; 
     $us_birthdate= $_POST['birthdate']; 
     $us_gender= $_POST['gender']; 
     $us_email_address= $_POST['email_address']; 
     $us_student_cell_number= $_POST['student_cell_number']; 
     $us_parents_cell_number= $_POST['parents_cell_number'];  
     $us_password1 = $_POST['password1']; 
     $us_password2 = $_POST['password2']; 
     mysqli_query($con,"INSERT INTO student_profile(student_id,user_id,name,permanent_address,local_address,password,birthdate,gender,email_address,student_cell_number,parents_cell_number,password1,password2) VALUES ('".$us_student_id."','".$us_user_id."','".$us_name."','".$us_permanent_address."','".$us_local_address."','".$us_birthdate."','".$us_gender."','".$us_email_address."','".$us_student_cell_number."','".$us_parents_cell_number."','".$us_password1."','".$us_password2.")"); 
     mysqli_close($con); 


     if (!mysqli_query($con,$sql)) 
     { 
     die('Error: redcord not added try again' . mysqli_error($con)); 
     } 

     echo "1 record added"; 
    ?>` 

這是我在數據庫使詞條編寫的代碼。表名是student_profile。字段如代碼所示。

我不能在數據庫表中輸入記錄,錯誤在輸出處顯示爲「未定義索引」。

+0

而錯誤是? – Goikiu

+0

undiefined indexlike「Undefined index:permanent_address in C:\ xamp2 \ htdocs \ rku \ userdatasave.php on line 15」 –

+0

因此var_dump()發佈數據並確保它實際上有一個'permanent_address'條目,注意拼寫:調試101 –

回答

3

請嘗試下面的代碼。

mysqli_query($con,"INSERT INTO `student_profile` (`student_id`,`user_id`,`name`,`permanent_address`,`local_address`,`password`,`birthdate`,`gender`,`email_address`,`student_cell_number`,`parents_cell_number`,`password1`,`password2`) VALUES ('$us_student_id','$us_user_id','$us_name','$us_permanent_address','$us_local_address','$us_birthdate','$us_gender','$us_email_address','$us_student_cell_number','$us_parents_cell_number','$us_password1','$us_password2')"); 
+0

我已嘗試此操作並且此操作適用於我但現在我得到像「未定義的變量:sql」,「mysqli_query():空查詢」和「mysqli_error():無法獲取mysqli」幫助我的錯誤 –

+0

我已經發布的答案,請找到它 –

1

如果你的數據庫表名和列名是正確的,那麼這應該工作:

mysqli_query($con,"INSERT INTO `student_profile` (`student_id`,`user_id`,`name`,`permanent_address`,`local_address`,`password`,`birthdate`,`gender`,`email_address`,`student_cell_number`,`parents_cell_number`,`password1`,`password2`) VALUES ('".$us_student_id."','".$us_user_id."','".$us_name."','".$us_permanent_address."','".$us_local_address."','".$us_birthdate."','".$us_gender."','".$us_email_address."','".$us_student_cell_number."','".$us_parents_cell_number."','".$us_password1."','".$us_password2."')"); 

只需添加`痕周圍表和列名和你的最後的值已丟失」。

可以肯定的是,您從表單中獲取值,POST方法。

+0

這是工作,但現在我得到像」未定義的變量:sql「,」mysqli_query():空查詢「和」mysqli_error():無法獲取mysqli「 –

+0

因爲你以後會這樣做:if(!mysqli_query($ con,$ sql)) 但是$ sql沒有價值。 – mkabanen

+0

你可以這樣解決: $ insertdb = mysqli_query(查詢我在我的答案中顯示..) 並檢查現在是否(!$ insertdb){error} else {good} – mkabanen

2

寫入查詢這種方式有助於調試和找到查詢錯誤,如由Fred-II提到你缺少報價

$qry = "INSERT INTO 
      `student_profile` 
      (
        `student_id`, 
        `user_id`, 
        `name`, 
        `permanent_address`, 
        `local_address`, 
        `password`, 
        `birthdate`, 
        `gender`, 
        `email_address`, 
        `student_cell_number`, 
        `parents_cell_number`, 
        `password1`, 
        `password2` 
      ) 
      VALUES 
      (
        '".$us_student_id."', 
        '".$us_user_id."', 
        '".$us_name."', 
        '".$us_permanent_address."', 
        '".$us_local_address."', 
        '".$us_birthdate."', 
        '".$us_gender."', 
        '".$us_email_address."', 
        '".$us_student_cell_number."', 
        '".$us_parents_cell_number."', 
        '".$us_password1."', 
        '".$us_password2."' 
      )" ; 

mysqli_query($con,$qry); 
+1

'mysql_real_escape_string' = **失敗**(我不是downvoter,順便說一句)但有人會。 –

+0

是的我已經更新,POST數據需要一些衛生,感謝提及它:) –

+0

充其量,你可以按照這個例子:'$ var = mysqli_real_escape_string($ con,$ _ POST ['var']);'並且不用客氣, –

1

這也許可能是由於你的HTML元素的name ,檢查HTML元素名稱和$ _POST ['元素名稱']是否相同。

+0

這是相同的,我已經檢查過它 –

1

試試這個:

<?php 
mysqli_query($con,"INSERT INTO student_profile(student_id,user_id,name,permanent_address,local_address,password,birthdate,gender,email_address,student_cell_number,parents_cell_number,password1,password2) VALUES ('$us_student_id','$us_user_id','$us_name','$us_permanent_address','$us_local_address','$us_birthdate','$us_gender','$us_email_address','$us_student_cell_number','$us_parents_cell_number','$us_password1','$us_password2')"); 
1

嗨未定義指數手段,有東西在陣列丟失。這意味着,無論你想從$ _POST陣列獲得什麼都不可用。

執行print_r($ _ POST)並檢查是否有調用正確的數組元素。

2

首先創建connection.php頁面並使用下面的代碼。

<?php 

$con = mysql_connect("localhost","root","") or die("can not connect"); 

$db = mysql_select_db("rku_database_main"); 

?> 

現在在您的頁面中添加該連接文件,其中包括您的上述代碼...使用下面的代碼添加連接文件。

include('connection.php'); 

包含此文件後,在下一行使用下面的代碼。

<?php 
$us_student_id= $_POST['student_id']; 
    $us_user_id= $_POST['user_id']; 
    $us_name= $_POST['name']; 
    $us_permanent_address= $_POST['permanent_address']; 
    $us_local_address= $_POST['local_address']; 
    $us_birthdate= $_POST['birthdate']; 
    $us_gender= $_POST['gender']; 
    $us_email_address= $_POST['email_address']; 
    $us_student_cell_number= $_POST['student_cell_number']; 
    $us_parents_cell_number= $_POST['parents_cell_number'];  
    $us_password1 = $_POST['password1']; 
    $us_password2 = $_POST['password2']; 

$sql1="INSERT INTO `student_profile` (`student_id`,`user_id`,`name`,`permanent_address`,`local_address`,`password`,`birthdate`, 
`gender`,`email_address`,`student_cell_number`,`parents_cell_number`,`password1`,`password2`) 
VALUES ('$us_student_id','$us_user_id','$us_name','$us_permanent_address','$us_local_address','$us_birthdate','$us_gender','$us_email_address','$us_student_cell_number','$us_parents_cell_number','$us_password1','$us_password2')"; 
$query1=mysql_query($sql1) or die("query failed1".mysql_error());          
相關問題