使用带有预处理语句的数组php,mysql,跳过空字符串

时间:2014-03-01 03:51:25

标签: php mysql sql

有一些问题需要学习mysql的预备语句。

我讨厌一切工作然后我遇到了一个新问题,最初的问题是我想跳过mysql更新表格中的空字符串(即:用户个人资料)。

我尝试以编程方式在php中执行此操作,但我不明白已经准备好的语句,我一直在阅读,但我没有成功,你能帮助我按照预期进行这项工作吗? Essentialy我试图在准备好的声明中使用数组。

if (empty($email) && empty($fullname)  && empty($address) && empty($country) && empty($state) && empty($city) && empty($postcode) && empty($phone) && empty($password) && empty($random_salt)) {
  echo "Nothing to do....";
  return;
}
else { 
    $state = $_POST['state'];
    $city = $_POST['city'];
    $postcode = $_POST['postcode'];
    $email = $_POST['email'];
    $fullname = $_POST['fullname'];
    $address = $_POST['address'];
    $country = $_POST['country'];
    $phone = $_POST['phone'];
    $merchantID = $_POST['merchantId'];

        // The hashed password from the form
    $password = $_POST['p']; 
    $pass2 = $password;
    $pass = $_POST['password'];

    $random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));

    $password = hash('sha512', $password.$random_salt);

    $updates = array();
    if (!empty($email))
      $updates[] = 'email="'.$email.'"';
    if (!empty($address))
      $updates[] = 'address="'.$address.'"';
    if (!empty($country))
      $updates[] = 'country="'.$country.'"';
    if (!empty($state))
      $updates[] = 'state="'.$state.'"';
    if (!empty($city))
      $updates[] = 'city="'.$city.'"';
    if (!empty($postcode))
      $updates[] = 'postcode="'.$postcode.'"';
    if (!empty($phone))
      $updates[] = 'phone="'.$phone.'"';
    if (!empty($password))
      $updates[] = 'password="'.$password.'"';
    if (!empty($random_salt))
      $updates[] = 'salt="'.$random_salt.'"';

    $updates = implode(', ', $updates);

  if ($update_stmt = $mysqli->prepare("UPDATE table SET ? WHERE id = ".$merchantID)) {
    $update_stmt->execute($updates);
    $update_stmt->close();
    //    
    echo '<br><p>';
    echo '<a href="homepage.com"> Account infomation update was a success...</a>';
  }
  else {
    echo "oppps, update didnt work, please report this to admin";
  }

} 

现在我觉得我已经误入歧途了,不管我为什么尝试我似乎无法解决如何做到这一点,因为我不能完全理解我尝试的准备好的陈述。

0 个答案:

没有答案