duanao4729 2016-12-31 01:33
浏览 56
已采纳

将sql转换为sqli pull id并使用php插入表中

I've been searching and trying for the last few hours with no luck. Trying to do this. I have a web Purchase order system. It uses mysql which is obviously outdated. It's a simple code, I pull the ID of the last ponumber, +1 to it, and reinsert it. I also display the new number on the form. This allows my system to display the PO number before submitting, and ensures noone else gets the same number twice. Here is my old code. There is a connection section in the header, but it's the same as the conn below which is all i havent added.

$row = mysql_fetch_array($result);
$ID = $row['ID'] +1 ;
?>
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("purchaseorder", $con);

$sql="INSERT INTO ponumber (ID)
VALUES
('$ID')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

?> 
<form action="submit.php" method="post" name="form" id="form" accept-    charset="utf-8">
<div class="form-all">
<ul class="form-section">
  <li id="cid_1" class="form-input-wide">
    <div class="form-header-group">
      <h2 id="header_1" class="form-collapse-table">
        Jillian Builders Purchase Order #:</h2><center><font size="9"> 
 <?php echo $ID ?>
      </h1></center></font></div>

This works just fine, other than an annoying sql is deprecated warning, which im trying to change. I cannot for the life of me figure out how to get the ID number using mysqli. I have tried numerous ways with fetch assoc, with array's, but for some reason I can't make it work. Im sure there is a better way to do this than what im doing. I'm open to suggestions. In the form. The employee needs to give the PO number to a vendor. Once he pushes submit on the form it submits the number in. So i need the number before he submits the data to the database so he can give it. Which is why i used +1. I get the old ID +1 to it to show the number in the FORM ONLY. It's not relevant to the actual DB. When he submits the PO it submits the number into it, and everything lines up. All i need to do it find a way to pull the ID off.

This is what i've tried so far to no avail

<?php 
$data = mysqli_query($conn,"SELECT ID FROM ponumber ORDER BY ID DESC LIMIT 1");
while ($row = mysqli_fetch_assoc ($data)) {
extract ($row);
echo "
ID=$ID +1 
";
}

Hope this helps clarify where i'm at Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongqu7778 2016-12-31 03:01
    关注

    First of all I'll need to tell you that your this code -( according to my understaning, if you are not using two databases)

    $row = mysql_fetch_array($result);
    $ID = $row['ID'] +1 ;
    

    should be after this code:

    <?php
    $con = mysql_connect("localhost","username","password");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("purchaseorder", $con);
    

    Please never do $row['ID'] +1 ;

    //just insert 
    $sql="INSERT INTO ponumber (ID)
    VALUES
    (NULL)";
    

    And the ID column of ponumber should be auto_incremented Then get the last insert id(auto incremented)

    $po_id = mysqli_insert_id($con); 
    

    in your form

    <?php echo $po_id ?>
    

    and one hidden input like

    <input type="hidden" name="po_id" value="<?php echo $po_id; ?>"/>
    

    And after submitting the form you should update the form with these values. And if you don't want the table to be empty (if user skips the full process of filling the form), just add a created_at column. After that you have updated the table, check if any row created one day ago exists, just delete it. For checking that if a row is empty or not add a flag column and play with it when going to update or delete the row -(means if a column named 'rec_inserted' is false, then this row should be deleted after 1 day. And this column should be set to true when going to update the record first time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试