2012-02-28 30 views
0

我有一個擁有'植物'主題的數據庫。我想要一個腳本來檢索玫瑰植物的細節並將這些值放入字段中。然後,我希望用戶能夠更新該項目並將其提交回數據庫。在窗體中使用SELECT和UPDATE查詢

我希望用戶從它的拉丁名數據庫這是在「玫瑰」表中的唯一屬性,拉項目。這裏是插入一個玫瑰的代碼,可能有人請幫助我適應這個吧,請使用預處理語句我多麼希望:

//connect to database 
$conn2 = DB2(); 

require_once('header_admin.php'); 
    /* 

if (isset($_POST['addrose'])) 
{ 

//detect if we have errors or not 
$errors = false; 
$error_msg = "Error, please try again"; 

/* 
* Could do other validation here 
* Make sure they enter an email address for example 
*/ 

//if we have no errors, do the SQL 
if (!$errors) { 

$latin_name = $_POST['latin_name']; 
$common_name = $_POST['common_name']; 
$variety_name = $_POST['variety_name']; 
$colour = $_POST['colour']; 
$season = $_POST['season']; 
$hardiness = $_POST['hardiness']; 
$situation = $_POST['situation']; 
$soil_type = $_POST['soil_type']; 
$price = $_POST['price']; 
$fragrance = $_POST['fragrance']; 
$height = $_POST['height']; 

//insert data 
$stmt = $conn2->prepare("INSERT INTO rosename (latin_name, common_name, variety_name, colour, season_of_interest, hardiness, situation, soil_type, price, 
fragrance, ultimate_height) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 

//bind the parameters 
    $stmt->bind_param('ssssssssdss', $latin_name, $common_name, $variety_name, $colour, $season, $hardiness, $situation, $soil_type, $price, $fragrance, 
    $height); 

// Execute query 
$stmt->execute(); 

//if the query worked, put out the confirmation message (you can make this look however you want) 
if ($stmt) { 
    echo "<p class='black'>Rose Added!</p>"; 
+0

爲什麼不使用框架? – Teson 2012-02-28 08:29:30

回答

1

我希望我得到它,你想要的東西。 這樣的:

if (!$errors) { 

    $latin_name = $_POST['latin_name']; 
    $stmt = $conn2->prepare("SELECT * FROM rosename WHERE latin_name = ?"); // replace SELECT * with SELECT field1, field2 (those you need) 
    $stmt->bind_param('s', $latin_name); 
    if ($result = $stmt->get_result()) { 
     /* fetch associative array */ 
     while ($row = $result->fetch_assoc()) { 
      printf ("%s (%s)\n", $row["soil_type"]); 
     } 
     /* free result set */ 
     $result->free(); 
    } 
} 

來自:http://php.net/manual/de/mysqli-result.fetch-assoc.php

希望幫助!

+0

謝謝,但這並不檢索數據到一個表單,並沒有使用準備好的語句 – user1227124 2012-02-28 08:53:58

+0

確定編輯它使用準備好的語句。 「將數據檢索到表單」是什麼意思?你可以輸出一些html標記 – Stefan 2012-02-28 08:58:25

+0

謝謝。這是否檢索數據並將其放入表單字段? – user1227124 2012-02-28 09:00:21