Solved

Creating Service tags

Posted on 2004-08-04
6
218 Views
Last Modified: 2008-03-03
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
Comment
Question by:livegirllove
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:Diggie
ID: 11724384
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
 
LVL 3

Expert Comment

by:pYrania
ID: 11724389
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
 
LVL 1

Author Comment

by:livegirllove
ID: 11730285
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 3

Accepted Solution

by:
Diggie earned 500 total points
ID: 11732361
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
 
LVL 3

Expert Comment

by:Diggie
ID: 11732371
one more thing:
use

error_reporting(E_ALL)

while testing to display any missing variables and more...
0
 
LVL 1

Author Comment

by:livegirllove
ID: 11747638
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now