2014-03-25 95 views
-4

有人可以請告訴我如何修改此代碼,而不是它它向觀察表添加一個新字段我希望它更新表,而不是用戶名等於:用戶和該child_ID等於:child_id多個MYSQL更新與PHP

<?php 

//load and connect to MySQL database stuff 
require("config.inc.php"); 

if (!empty($_POST)) { 
//initial query 
$query = "INSERT INTO observation (username, elg1, elg2, elg3, elg4, elg5,elg6, elg7, elg8, elg9, elg10, elg11, elg12, elg13, elg14, elg15, elg16, elg17, col1, col2, col3, child_id) VALUES (:user, :elg1, :elg2, :elg3, :elg4, :elg5, :elg6, :elg7, :elg8, :elg9, :elg10, :elg11, :elg12, :elg13, :elg14, :elg15, :elg16, :elg17, :col1, :col2, :col3, :child_id)"; 


//Update query 
$query_params = array(
    ':user' => $_POST['username'], 
    ':elg1' => $_POST['elg1'], 
':elg2' => $_POST['elg2'], 
':elg3' => $_POST['elg3'], 
':elg4' => $_POST['elg4'], 
':elg5' => $_POST['elg5'], 
':elg6' => $_POST['elg6'], 
':elg7' => $_POST['elg7'], 
':elg8' => $_POST['elg8'], 
':elg9' => $_POST['elg9'], 
':elg10' => $_POST['elg10'], 
':elg11' => $_POST['elg11'], 
':elg12' => $_POST['elg12'], 
':elg13' => $_POST['elg13'], 
':elg14' => $_POST['elg14'], 
':elg15' => $_POST['elg15'], 
':elg16' => $_POST['elg16'], 
':elg17' => $_POST['elg17'], 
    ':col1' => $_POST['col1'], 
':col2' => $_POST['col2'], 
    ':col3' => $_POST['col3'], 
':child_id' => $_POST['child_id'] 


); 

//execute query 
try { 
    $stmt = $db->prepare($query); 
    $result = $stmt->execute($query_params); 
} 
catch (PDOException $ex) { 
    // For testing, you could use a die and message. 
    //die("Failed to run query: " . $ex->getMessage()); 

    //or just use this use this one: 
    $response["success"] = 0; 
    $response["message"] = "Database Error. Couldn't add post!" . $ex->getMessage(); 
    die(json_encode($response)); 
} 

$response["success"] = 1; 
$response["message"] = "Post Successfully Added!"; 
echo json_encode($response); 

} else { 
?> 
    <h1>Add Comment</h1> 
    <form action="elg.php" method="post"> 
     Username:<br /> 
     <input type="text" name="username" placeholder="username" /> 
     <br /><br /> 
     elg1:<br /> 
     <input type="text" name="elg1" placeholder="elg1" /> 
     <br /><br /> 
     elg2:<br /> 
     <input type="text" name="elg2" placeholder="elg2" /> 
     <br /><br /> 
     elg3:<br /> 
     <input type="text" name="elg3" placeholder="elg3" /> 
     <br /><br /> 
     elg4:<br /> 
     <input type="text" name="elg4" placeholder="elg4" /> 
     <br /><br /> 
     elg5:<br /> 
     <input type="text" name="elg5" placeholder="elg5" /> 
     <br /><br /> 
     elg6:<br /> 
     <input type="text" name="elg6" placeholder="elg6" /> 
     <br /><br /> 
     elg7:<br /> 
     <input type="text" name="elg7" placeholder="elg7" /> 
     <br /><br /> 
     elg8:<br /> 
     <input type="text" name="elg8" placeholder="elg8" /> 
     <br /><br /> 
     elg9:<br /> 
     <input type="text" name="elg9" placeholder="elg9" /> 
     <br /><br /> 
     elg10:<br /> 
     <input type="text" name="elg10" placeholder="elg10" /> 
     <br /><br /> 
     elg11:<br /> 
     <input type="text" name="elg11" placeholder="elg11" /> 
     <br /><br /> 
     elg12:<br /> 
     <input type="text" name="elg12" placeholder="elg12" /> 
     <br /><br /> 
     elg13:<br /> 
     <input type="text" name="elg13" placeholder="elg13" /> 
     <br /><br /> 
     elg14:<br /> 
     <input type="text" name="elg14" placeholder="elg14" /> 
     <br /><br /> 
     elg15:<br /> 
     <input type="text" name="elg15" placeholder="elg15" /> 
     <br /><br /> 
     elg16:<br /> 
     <input type="text" name="elg16" placeholder="elg16" /> 
     <br /><br /> 
     elg17:<br /> 
     <input type="text" name="elg17" placeholder="elg17" /> 
     <br /><br /> 
     col1:<br /> 
     <input type="text" name="col1" placeholder="col1" /> 
     <br /><br /> 
     col2:<br /> 
     <input type="text" name="col2" placeholder="col2" /> 
     <br /><br /> 
     col3:<br /> 
     <input type="text" name="col3" placeholder="col3" /> 
     <br /><br /> 
     child_id:<br /> 
     <input type="text" name="child_id" placeholder="child_id" /> 
     <br /><br /> 




     <input type="submit" value="Add Comment" /> 
    </form> 
<?php 
} 

?> 
+0

要更新和要更新 – user3004356

+1

什麼做到這一點是很基本的SQL哪裏。你有沒有嘗試過任何東西?查看手冊:http://dev.mysql.com/doc/refman/5.0/en/update.html – jeroen

回答

0

試試這個:

<?php 

//load and connect to MySQL database stuff 
require("config.inc.php"); 

if (!empty($_POST)) { 
//initial query 
$query = "UPDATE observation SET elg1 = :elg1, elg2 = :elg2, elg3 = :elg3, elg4 = :elg4, elg5 = :elg5, elg6 = :elg6, elg7 = :elg7, elg8 = :elg8, elg9 = :elg9, elg10 = :elg10, elg11 = :elg11, elg12 = :elg12, elg13 = :elg13, elg14 = :elg14, elg15 = :elg15, elg16 = :elg16, elg17 = :elg17, col1 = :col1, col2 = :col2, col3 = :col3 WHERE username = :user AND child_id = :child_id"; 

//Update query 
$query_params = array(
    ':user' => $_POST['username'], 
    ':elg1' => $_POST['elg1'], 
':elg2' => $_POST['elg2'], 
':elg3' => $_POST['elg3'], 
':elg4' => $_POST['elg4'], 
':elg5' => $_POST['elg5'], 
':elg6' => $_POST['elg6'], 
':elg7' => $_POST['elg7'], 
':elg8' => $_POST['elg8'], 
':elg9' => $_POST['elg9'], 
':elg10' => $_POST['elg10'], 
':elg11' => $_POST['elg11'], 
':elg12' => $_POST['elg12'], 
':elg13' => $_POST['elg13'], 
':elg14' => $_POST['elg14'], 
':elg15' => $_POST['elg15'], 
':elg16' => $_POST['elg16'], 
':elg17' => $_POST['elg17'], 
    ':col1' => $_POST['col1'], 
':col2' => $_POST['col2'], 
    ':col3' => $_POST['col3'], 
':child_id' => $_POST['child_id'] 


); 

//execute query 
try { 
    $stmt = $db->prepare($query); 
    $result = $stmt->execute($query_params); 
} 
catch (PDOException $ex) { 
    // For testing, you could use a die and message. 
    //die("Failed to run query: " . $ex->getMessage()); 

    //or just use this use this one: 
    $response["success"] = 0; 
    $response["message"] = "Database Error. Couldn't add post!" . $ex->getMessage(); 
    die(json_encode($response)); 
} 

$response["success"] = 1; 
$response["message"] = "Post Successfully Added!"; 
echo json_encode($response); 

} else { 
?> 
    <h1>Add Comment</h1> 
    <form action="elg.php" method="post"> 
     Username:<br /> 
     <input type="text" name="username" placeholder="username" /> 
     <br /><br /> 
     elg1:<br /> 
     <input type="text" name="elg1" placeholder="elg1" /> 
     <br /><br /> 
     elg2:<br /> 
     <input type="text" name="elg2" placeholder="elg2" /> 
     <br /><br /> 
     elg3:<br /> 
     <input type="text" name="elg3" placeholder="elg3" /> 
     <br /><br /> 
     elg4:<br /> 
     <input type="text" name="elg4" placeholder="elg4" /> 
     <br /><br /> 
     elg5:<br /> 
     <input type="text" name="elg5" placeholder="elg5" /> 
     <br /><br /> 
     elg6:<br /> 
     <input type="text" name="elg6" placeholder="elg6" /> 
     <br /><br /> 
     elg7:<br /> 
     <input type="text" name="elg7" placeholder="elg7" /> 
     <br /><br /> 
     elg8:<br /> 
     <input type="text" name="elg8" placeholder="elg8" /> 
     <br /><br /> 
     elg9:<br /> 
     <input type="text" name="elg9" placeholder="elg9" /> 
     <br /><br /> 
     elg10:<br /> 
     <input type="text" name="elg10" placeholder="elg10" /> 
     <br /><br /> 
     elg11:<br /> 
     <input type="text" name="elg11" placeholder="elg11" /> 
     <br /><br /> 
     elg12:<br /> 
     <input type="text" name="elg12" placeholder="elg12" /> 
     <br /><br /> 
     elg13:<br /> 
     <input type="text" name="elg13" placeholder="elg13" /> 
     <br /><br /> 
     elg14:<br /> 
     <input type="text" name="elg14" placeholder="elg14" /> 
     <br /><br /> 
     elg15:<br /> 
     <input type="text" name="elg15" placeholder="elg15" /> 
     <br /><br /> 
     elg16:<br /> 
     <input type="text" name="elg16" placeholder="elg16" /> 
     <br /><br /> 
     elg17:<br /> 
     <input type="text" name="elg17" placeholder="elg17" /> 
     <br /><br /> 
     col1:<br /> 
     <input type="text" name="col1" placeholder="col1" /> 
     <br /><br /> 
     col2:<br /> 
     <input type="text" name="col2" placeholder="col2" /> 
     <br /><br /> 
     col3:<br /> 
     <input type="text" name="col3" placeholder="col3" /> 
     <br /><br /> 
     child_id:<br /> 
     <input type="text" name="child_id" placeholder="child_id" /> 
     <br /><br /> 




     <input type="submit" value="Add Comment" /> 
    </form> 
<?php 
} 

?> 
1

你基本上可以使用相同的語句。但將語法更新爲UPDATE語句。

$query = "UPDATE observation SET elg1=:elg1,elg2=:elg2,[etc] where child_id=:child_id and user=:user";