在mysql中更新表

时间:2013-01-05 10:35:50

标签: php mysql

我计划通过使用这些代码行来更新我的sql数据库

  <?php
  //session_start();
  $user=$_SESSION['user_level'];
  // Check if a file has been uploaded
  if(isset($_FILES['fileToUpload'])) {
  // Make sure the file was sent without errors
  if($_FILES['fileToUpload']['error'] == 0) {
    // Connect to the database
    $dbLink = new mysqli('$host', '$user', '$pass', 
    '$tbl_name');
    if(mysqli_connect_errno()) {
        die("MySQL connection failed: ". mysqli_connect_error());
    }

    // Gather all required data
    //$id= mysql_insert_id(); 
    $name = $dbLink->real_escape_string($_FILES['fileToUpload']['name']);
    $mime = $dbLink->real_escape_string($_FILES['fileToUpload']['type']);
    $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['fileToUpload']
    ['tmp_name']));
    $size = intval($_FILES['fileToUpload']['size']);
    // Create the SQL query
    $query = "
        UPDATE userinfo SET resume=$name
            WHERE FirstName=$user";
    // Execute the query
    $result = $dbLink->query($query);}}
    ?>
    <?php 


    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],   
    "/home/u152912911/public_html/upload/" . $_FILES["fileToUpload"]["name"]);
    ?>

    <?php
    if ($_FILES["fileToUpload"]["error"] > 0)
    {
     echo "Apologies, an error has occurred.";
     echo "Error Code: " . $_FILES["fileToUpload"]["error"];
    }
    else
    {
    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
    "/home/u152912911/public_html/upload/" . $_FILES["fileToUpload"]["name"]);
    }
    if (($_FILES["fileToUpload"]["type"] == "image/DOC")
    || ($_FILES["fileToUpload"]["type"] == "image/jpeg")
    || ($_FILES["fileToUpload"]["type"] == "image/png" )
    && ($_FILES["fileToUpload"]["size"] < 10000))
    {
    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
    "/home/u152912911/public_html/upload/" . $_FILES["fileToUpload"]["name"]);

ECHO "Files Uploaded Succesfully";
echo'<script type="text/javascript">

    window.location.href ="resume2.php"
    </script>';
    }
    else
    {
    }
    echo "Your Resume was Successfully Upload";
    ?>
问题是它不起作用。我的简历专栏没有改变。我错过了什么吗?因为它不会显示任何错误。提前谢谢你!

2 个答案:

答案 0 :(得分:1)

您只需使用以下

$query = "UPDATE userinfo SET resume='$name' WHERE FirstName='$user'";

而不是

$query = "UPDATE userinfo SET resume=$name WHERE FirstName=$user";

试试吧。它可能有用

答案 1 :(得分:0)

请尝试以下操作:

PHP部分:

<?php
$host = 'Your Host Name';
$user = 'Your Database Username';
$pass = 'Your Database Password';
$db_name = 'Your Database Name';
$first_name = 'john';//Here your session user firstname

//Check if a file has been uploaded

if(isset($_FILES['fileToUpload'])) {

// Connect to the database
$dbLink = new mysqli(''.$host.'', ''.$user.'', ''.$pass.'',''.$db_name.'');

/*
 * This is the "official" OO way to do it,
 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
 */
if ($dbLink->connect_error) {
    die('Connect Error (' . $dbLink->connect_errno . ') '. $dbLink->connect_error);
}

$name       = $_FILES['fileToUpload']['name'];
$mime       = $_FILES['fileToUpload']['type'];
$temp_name  = $_FILES['fileToUpload']['tmp_name'];
$size       = intval($_FILES['fileToUpload']['size']);
$first_name = $_POST['first_name'];

if(($_FILES["fileToUpload"]["type"] == "image/DOC") || ($_FILES["fileToUpload"]["type"] == "image/jpeg") || ($_FILES["fileToUpload"]["type"] == "image/png" ) && ($_FILES["fileToUpload"]["size"] < 10000)) {

 // Create the SQL query
 $query = "UPDATE `userinfo` SET `resume`='$name' WHERE `FirstName`='$first_name'";
 // Execute the query
 $result = $dbLink->query($query);

 move_uploaded_file($temp_name,"gallery3/".$name);

 echo "Files Uploaded Succesfully";

}
else {
 echo "Apologies, an error has occurred.";
 echo "Error Code: " . $_FILES["fileToUpload"]["error"];
}
}
?>

HTML部分:

<form action="" method="post" enctype="multipart/form-data" name="fileupload">
<input type="file" name="fileToUpload">
<input type="hidden" name="first_name" value="<?php echo $first_name;?>">
<input type="submit" name="uploading" value="File Upload">
</form>

我认为这可以帮助您解决问题。