dongtui6347 2014-05-21 22:34
浏览 34
已采纳

问题与mysql_query INSERT(PHP 5.3)没有添加到表

I have an job application form that sends an email with the information, and should also insert the values from the form (the values of which are imported into the handler.php seen below at the top) into a table in a database on the server.

However, it's not inserting the values, and I can't figure out why.

I actually took this script from my client's old version of the website on a different domain and server, and it's still working perfectly on that one, so I can't figure out why it's not working here. The only thing I've changed is the database details. At one point I thought the issue might be because mysql_connect etc. is deprecated in favour of mysqli-equivalents, but the client's server is still running PHP 5.3, so that shouldn't be the issue.

Via PHPMyAdmin, I have a database, and a table called 'team'. I create all the columns according to the original setup on the old website, so that each item you see in the INSEERT query down below will have somewhere to go.

Am I missing something in my SQL connection code? The username, password, domain and database are all correct...

$date = date("d/M/Y");
$name = Trim(stripslashes($_POST['name'])); 
$dob = Trim(stripslashes($_POST['dob']));
$contact = Trim(stripslashes($_POST['contact']));
$email = Trim(stripslashes($_POST['email'])); 
$preferredCity = Trim(stripslashes($_POST['preferredCity']));
$nightsMo = Trim(stripslashes($_POST['nightsMo']));
$nightsTu = Trim(stripslashes($_POST['nightsTu']));
$nightsWe = Trim(stripslashes($_POST['nightsWe']));
$nightsTh = Trim(stripslashes($_POST['nightsTh']));
$nightsFr = Trim(stripslashes($_POST['nightsFr']));
$nightsSa = Trim(stripslashes($_POST['nightsSa']));
$nightsSu = Trim(stripslashes($_POST['nightsSu']));
$ownCar = Trim(stripslashes($_POST['ownCar']));
$previousWork = Trim(stripslashes($_POST['previousWork']));

if (Trim($nightsMo)=="") $nightsMo="NULL";
if (Trim($nightsTu)=="") $nightsTu="NULL";
if (Trim($nightsWe)=="") $nightsWe="NULL";
if (Trim($nightsTh)=="") $nightsTh="NULL";
if (Trim($nightsFr)=="") $nightsFr="NULL";
if (Trim($nightsSa)=="") $nightsSa="NULL";
if (Trim($nightsSu)=="") $nightsSu="NULL";
if (Trim($ownCar)=="") $ownCar="NULL";

$username="XXX";
$password="XXX";
$database="XXX";


mysql_connect("example.co.uk",$username,$password);

@mysql_select_db($database) or die( "Unable to select database");


mysql_query("INSERT INTO team (t_date, t_name, t_dob, t_contact, t_email, t_preferredCity, t_nightsMo, t_nightsTu, t_nightsWe, t_nightsTh, t_nightsFr, t_nightsSa, t_nightsSu, t_ownCar, t_previousWork, t_picture) VALUES ('$date', '$name','$dob','$contact','$email','$preferredCity',$nightsMo,$nightsTu,$nightsWe,$nightsTh,$nightsFr,$nightsSa,$nightsSu,$ownCar,'$previousWork', '$filePath')");

mysql_close();
  • 写回答

1条回答 默认 最新

  • douwuying4709 2014-05-21 23:10
    关注

    Unclear what could be the cause, but refactored your code to streamline & simplify things so it’s easier to debug.

    The first thing I did was take all of your $_POST checks into a structure that uses one main array ($post_array) and then rolls through that array to process the values & assign them to similarly named variables.

    Additionally, I added a $result string check for the mysql_query which then does a check to see if that is even returned. And if it fails, show us what the error is via mysql_error(). That should be the key to help you debug what is happening.

    $date = date("d/M/Y");
    
    // Set the post values array.
    $post_array('name','dob','contact','email','preferredCity','nightsMo','nightsTu','nightsWe','nightsTh','nightsFr','nightsSa','nightsSu');
    
    // Role through the post values array.
    foreach($post_array as $post_key => $post_value) {
      $$post_key = isset($_POST[$post_key]) ? Trim(stripslashes($_POST[$post_key])) : null; 
    }
    
    $username="XXX";
    $password="XXX";
    $database="XXX";
    
    mysql_connect("example.co.uk",$username,$password);
    
    @mysql_select_db($database) or die( "Unable to select database");
    
    $query = "INSERT INTO team (t_date, t_name, t_dob, t_contact, t_email, t_preferredCity, t_nightsMo, t_nightsTu, t_nightsWe, t_nightsTh, t_nightsFr, t_nightsSa, t_nightsSu, t_ownCar, t_previousWork, t_picture) VALUES ('$date', '$name','$dob','$contact','$email','$preferredCity','$nightsMo','$nightsTu','$nightsWe','$nightsTh','$nightsFr','$nightsSa','$nightsSu','$ownCar','$previousWork', '$filePath')";
    
    // Run the query & return the result.
    $result = mysql_query($query);
    
    // Check the mysql query result. If there is none, there is an error. So tell us what the error is.
    if (!$result) {
      die('Invalid query: ' . mysql_error());
    }
    
    mysql_close();
    

    Also, unrelated but worth pointing out is mysql_* extensions are depreciated in PHP 5.3 & PHP 5.4. You should switch to mysqli_* extensions since mysql_* extensions with be eliminated in PHP 5.5.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址