• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Creating Service tags

Hi All-
I have a MySQL db with drivers in it.  I want to create service tag numbers for the computers I build for people so they can go online and download drivers.  
This is the way I select which driver I want for each category.  How would I then take the values chosen by the drop down boxes and insert them into another table, and returning the id number of the row they where inserted into.  That  auto-increment id number will be the service tag number.  Later I can use that number to pull the data back on on a website for the customer.

  Modem Driver</p>
  <p>
    <select name="category" size="1">
      <?php
include("db.php"); // setup connection to mysql

$sql = "SELECT name FROM drivers WHERE cat='SCSI'";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
    echo "<option>".$row['name']."</option>\n";
}
?>
    </select>

Maybe something like this:
  <?
include("db.php"); // setup connection to mysql
if($_POST["submit"]) { // if form submitted
$scsilink = $_POST['link'];
$scsiname = $_POST['name'];
$query = "INSERT INTO servicetag (scsilink,scsiname,lanlink, lanname) VALUES ('$scsilink','$scsiname','$,lanlink')";

 // update your field in your table
  $result = mysql_query($query) or die(mysql_error()); // exec query
  print "Service Tag Created";
}
$r = mysql_query($q) or die(mysql_error()); // exec
$row = mysql_fetch_assoc($r); // fetch the row

?>


i haven't created the servicetag table yet because I'm not sure about the best way to structure it.

any ideas would be great,

thanks
0
livegirllove
Asked:
livegirllove
  • 3
  • 2
1 Solution
 
DiggieCommented:
use the mysql_insert_id() function to get the id of the last inserted row...

more info: http://nl3.php.net/manual/en/function.mysql-insert-id.php
0
 
pYraniaCommented:
mysql_insert_id() is what you need. it will return the id of the last inserted row.

<?php
include("db.php"); // setup connection to mysql

if($_POST["submit"]) { // if form submitted
    $scsilink = $_POST['link'];
    $scsiname = $_POST['name'];
    $query = "INSERT INTO servicetag (scsilink,scsiname,lanlink, lanname) VALUES ('$scsilink','$scsiname','$lanlink')";
    $result = mysql_query($query) or die(mysql_error()); // exec query
    print "Service Tag Created";
    print "Service ID: ". mysql_insert_id();
}

?>
0
 
livegirlloveAuthor Commented:
I get an error:You have an error in your SQL syntax near ') VALUES ('','')' at line 1
I need to select a driver by name, (That works) and on submit it takes the name of the driver and puts it into servicetag.vidname and the link that is in the same row as the name that I selected and puts that link into servicetag.vid

Thanks,

<form action="<? print $_SERVER["../PHP_SELF"]; ?>" method="POST">
  <p>
    <select name="category" size="1">
      <?php
include("db.php"); // setup connection to mysql

$sql = "SELECT name FROM drivers WHERE cat='Video'";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
    echo "<option>".$row['name']."</option>\n";
}
?>
    </select>
  Video Driver</p>
              <input type="submit" name="submit" value="Create Service Tag">
              <input type="hidden" name="submit" value="Update Drivers">
           
</form>
<?php
include("db.php"); // setup connection to mysql
if($_POST["submit"]) { // if form submitted
    $vid = $_POST['vid'];
    $vidname = $_POST['vidname'];
    $query = "INSERT INTO servicetag (vid,vidname,) VALUES ('$vid','$vidname')";
    $result = mysql_query($query) or die(mysql_error()); // exec query
    print "Service Tag Created";
    print "Service ID: ". mysql_insert_id();
}

?><p>&nbsp;
           
</p>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DiggieCommented:
ths sql error is here:
$query = "INSERT INTO servicetag (vid,vidname,) VALUES ('$vid','$vidname')";
                                                               ----^----

do yourself a favor an try to spit logic (code) and output (html)!!!

Anyway, I suppose ypur code above is not complete because the only variable posted (beside 2 x submit?) is 'category'... Which is not used in the if($_POST["submit"]) section

nofi, but from the looks of your code you are not yet at ease with either html, php or databases...

- you don't need to have both vid and vidname in the servicetag table (this is called normalization, google it up for more information)
- use logical names for your input fields; not category <-> vidname (this will confuse even the author of the code)
- use the value tag of <option> to pass a value other than the text
etc...

below a (not tested and slightly intoxinated!) version of the code above:

<?php

      $msg = ''; // default empty message

      include("db.php"); // setup connection to mysql
      
      $sql = "SELECT id, name FROM drivers WHERE cat='Video'";
      $res_cat = mysql_query($sql); // handle to category set
      
      if (!empty($_POST)) { // form is subitted
            $vid = $_POST['vid']; // id of vid cat
            $sql = "INSERT INTO servicetag (vid) VALUES ('$vid')";
            $res_add = mysql_query($sql) or die (mysql_error()); // exec query
            $msg  = "Service Tag Created<br>";
            $msg .= "Service ID: ". mysql_insert_id();
      }

?>

<form action="" method="POST">
      <p><?php echo $msg; ?></p>
      <p>
      <select name="vid">
      <?php while ($row = mysql_fetch_assoc($res_cat)) { ?>
            <option value='<?php echo $row[id]; ?>'><?php echo $row['name']; ?></option>
      <?php } ?>
      </select>
      Video Driver
      </p>
      <input type="submit" name="submit" value="Create Service Tag">
      <input type="hidden" name="my_action" value="Update Drivers">
           
</form>
0
 
DiggieCommented:
one more thing:
use

error_reporting(E_ALL)

while testing to display any missing variables and more...
0
 
livegirlloveAuthor Commented:
The above code works perfectly.  Now I'm just figureing out how to enter the other drivers while creating the service tag.

I'm very comfortable with HTML, but I'm just learning php/mysql

""you don't need to have both vid and vidname in the servicetag table (this is called normalization, google it up for more information)""
Yes, that's the logic I wasn't getting my head around.  

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now