2014-08-27 86 views
-1

所以我試圖讓AA網店,基本上是不工作是執行查詢時買進點擊「買入」 button.The查詢:如何通過點擊PHP中的按鈕觸發一個SQL查詢?

$sql = mysql_query("INSERT INTO vehicles (model,owner) VALUES ('$vehid','$id')"); 

和按鈕

<form action=\"\" method=\"post\"> 
    <input type=\"submit\" value=\"BUY\"> 
</form> 

整個代碼:

<?php 
$id = $_SESSION['SESS_MEMBER_ID']; 


include ('config2.php'); 

$result = mysql_query("select * from shop_vehicule ORDER BY id DESC"); 
$result2 = mysql_query("select * from accounts where id = '$id'"); 
while($row = mysql_fetch_array($result2)) 
$credit = $row['credits']; 
while($row = mysql_fetch_array($result)){ 
    $name = $row['nume']; 
    $price = $row['pret']; 
    $left = $credit - $price; 
    $vehid = $row['vehid']; 

    echo "<p><center><b>$name</b> | $price </center> 
     <a href=\"#\" class=\"topopup\">More information about $name</a></p> 

    <div id=\"toPopup\"> 

     <div class=\"close\"></div> 
     <span class=\"ecs_tooltip\">Press Esc to close <span class=\"arrow\"></span></span> 
     <div id=\"popup_content\"> <!--your content start--> 
      <p> 
The $name costs $price, after you'll have $left !</p> 

<form action=\"\" method=\"post\"> 
    <input type=\"submit\" value=\"BUY\"> 
</form> 

     </div> 

    </div> 

    <div class=\"loader\"></div> 
    <div id=\"backgroundPopup\"></div>"; 
$sql = mysql_query("INSERT INTO vehicles (model,owner) VALUES ('$vehid','$id')"); 
} 

mysql_close(); 
?> 
+0

你問不好的問題,並有可能失去你的問題 - 要求特權。 [您應該在發佈下一個之前閱讀此內容。](http://s.tk/onhold) – 2014-08-27 17:51:59

+1

'mysql_query'是一個過時的界面,不應在新應用程序中使用,並且將在未來版本的PHP中刪除。像[PDO這樣的現代化替代品並不難學](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/)。如果您是PHP的新手,像[PHP The Right Way](http://www.phptherightway.com/)這樣的指南可以幫助解釋最佳實踐。 – tadman 2014-08-27 18:12:03

+1

你所提問題的種類表明缺乏基礎知識。你對PHP有什麼參考?一本好書或參考網站將大大緩解你的學習。 – tadman 2014-08-27 18:13:30

回答

0

用行動= $ _ SERVER [ 'PHP_SELF']的形式標記,並進行寫MySQL的插入代碼的條件,其中isset($ _ POST ['購買']) 是真的。

-1

你可以在php中做到這一點,但在2個不同的文件。 第一個將具有形式,並且所述第二將讀取POST值和執行查詢

實施例(請填寫缺少的部分)

文件1。 PHP

<form action="file2.php" method="post"> 
     <input type="hidden" value=<?php echo $vehid;?>" name="vehid"> 
     <input type="hidden" value=<?php echo $id;?>" name="id"> 
     <input type="submit" value="BUY"> 
    </form> 

File2.php

$vehid=$_POST['model']; 
$id=$_POST['id']; 
$sql = mysql_query("INSERT INTO vehicles (model,owner) VALUES ('$vehid','$id')"); 

對於一個完整的教程,請參閱http://www.w3schools.com/php/php_mysql_insert.asp

+0

請[不要鏈接到w3schools](http://w3fools.com/)。這有許多有害的過時示例,並使一些有害的編程習慣永久化。它確實有害無益。 – tadman 2014-08-27 20:23:02

1

這裏是我試圖幫助,我沒有測試代碼,但它應該是工作。請閱讀代碼中的評論。它解釋了它的作用。

$id = $_SESSION['SESS_MEMBER_ID']; 

/* To use PDO the following line must be included in your config2.php 

    define('DB_HOST', 'localhost'); 
    define('DB_NAME', 'database'); 
    define('DB_USER', 'username'); 
    define('DB_PASS', 'password'); 
    $db = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME, DB_USER, DB_PASS); 

    You can either use define or put the info straight into the PDO() function but I like it when it's easy to read and modify if needed. 
*/ 
include ('config2.php'); 

$query = $db->prepare("SELECT * FROM accounts WHERE id = :id"); //Please use PDO or MySQLi, MySQL is outdated and unsecure. For this example, I am using my favorite method which is PDO. 
$query->execute(array(':id' => $id)); 
$account = $query->fetchObject(); //Since we only need one line, we're going to use fetchObject object. 

$query2 = $db->prepare("SELECT * FROM shop_vehicule ORDER BY id DESC"); 
$query2->execute(); 
$vehicules = $query2->fetchAll(); //I am using fetchAll due to multiple row will be returned. 

foreach ($vehicules as $row) { 
    echo '<p><center><b>'.$row['nume'].'</b> | '.$row['pret'].' </center> 
     <a href="#" class="topopup">More information about $name</a></p> 

    <div id="toPopup"> 
     <div class="close"></div> 
     <span class="ecs_tooltip">Press Esc to close <span class="arrow"></span></span> 
     <div id="popup_content"> <!--your content start--> 
      <p>The '.$row['nume'].' costs '.$row['pret'].', after you\'ll have '.$account->credit - $row['pret'].' !</p> 
      <a href="?purchase='.$row['vehid'],'">BUY</a> 
     </div> 
    </div> 
    <div class="loader"></div> 
    <div id="backgroundPopup"></div>'; 
} 

// Basically what this part does is whenever the user click on the link, purchase will be set and it'll trigger the query to insert into the vehicule table then return a message if it was successful or not. 
if (isset($_GET['purchase'])) { 
    $query = $db->prepare("INSERT INTO vehicles (model,owner) VALUES (':vehid',':id');"); 
    $query->execute(array(':vehid' => $_GET['purchase'], ':id' => $id)); 

    if ($query) { 
     echo 'Congratulations! You have successfully purchased the vehicule!'; 
    } else { 
     echo 'An error has occured, the purchase was not complete.'; 
    } 
} 
+0

僅通過切換到PDO看起來好多了。 – tadman 2014-08-27 20:21:54