2014-08-27 25 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