Thread: Id question

    #1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Posts
    650
    Rep Power
    20

    Id question


    hey guys im blocked at the adding an id in the db?

    lets say my form inserts

    NSERT INTO `techker_cars`.`cars` (
    `id` ,
    `make` ,
    `model` ,
    `year` ,
    `kilometers` ,
    `description` ,
    `inventory` ,
    `price` ,
    `photo` ,
    `thumb` ,
    `other_pics`
    )
    VALUES (
    '1', 'Honda', 'civic ex', '2000', '200000', 'testing again here', '1', '12000$', '', '', ''
    );

    this was done directly in phpmyadmin so i can get an idea how it iserts..

    but were i block is how or what code to i add to my page to generate an id to insert?
    1', 'Honda', 'civic ex', '2000', '200000', 'testing again here', '1', '12000$', '', '', ''
  2. #2
  3. No Profile Picture
    Hardrocking Amigo
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2004
    Location
    Sydney, Australia
    Posts
    601
    Rep Power
    105
    You dont generate it in your code.
    You add an attribute of 'auto_increment' to the id column and it increases as records are inserted, its done automatically. You dont even need to add the id to your insert query.

    Hope that helps,

    Christian
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Posts
    650
    Rep Power
    20
    i did add the auto increment and the primary key to a field called id

    but when i make a form to insert data nothing happens?when i delete that id table it works?

    i added null and still nothing??
  6. #4
  7. No Profile Picture
    Hardrocking Amigo
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2004
    Location
    Sydney, Australia
    Posts
    601
    Rep Power
    105
    Dont add anything at all....

    PHP Code:
    NSERT INTO `techker_cars`.`cars` (`make` ,`model` ,`year` ,`kilometers` ,`description` ,`inventory` ,`price` ,`photo` ,`thumb` ,`other_pics`) VALUES ('Honda''civic ex''2000''200000','testing again here''1''12000$'''''''); 
    Christian
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Posts
    650
    Rep Power
    20
    i have to be doing something wrong cause nothing works?

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <
    title>Untitled Document</title>
    </
    head>

    <
    body>
    <
    form id="form1" name="form1" method="post" action="add2.php">
      <
    p>
        <
    input type="text" name="name" id="textfield" /> 
        
    name
    </p>
      <
    p>
        <
    input type="text" name="email" id="textfield2" />
      
    email</p>
      <
    p>
        <
    input type="text" name="job" id="textfield3" /> 
        
    job
    </p>
      <
    p>
        <
    input type="submit" name="button" id="button" value="Submit" />
      </
    p>
    </
    form>
    </
    body>
    </
    html
    PHP Code:
     and add2.php
    <?
    $name
    =$_POST['name'];
    $email=$_POST['email'];
    $job=$_POST['job'];
    $db="test";
    //$link = mysql_connect("localhost");
    $link mysql_connect("localhost",$_POST['root'],$_POST['']);
    if (! 
    $link)
    die(
    "Couldn't connect to MySQL");
    mysql_select_db($db $link) or die("Select Error: ".mysql_error());
    $result=mysql_query("INSERT INTO `test`.`jobs`(name, email,job) VALUES ('$name','$email','$job')")or die("Insert Error: ".mysql_error());
    mysql_close($link);
    print 
    "Record added\n";
    ?>
  10. #6
  11. Permanently Banned
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2006
    Location
    In a whale
    Posts
    4,127
    Rep Power
    0
    PHP Code:
    $link mysql_connect("localhost",$_POST['root'],$_POST['']); 
    *double take*

    What?! $_POST['root'] and an empty $_POST? Do you mean strings? Ouch, that is what is killing you.
  12. #7
  13. No Profile Picture
    Hardrocking Amigo
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2004
    Location
    Sydney, Australia
    Posts
    601
    Rep Power
    105
    yeah, please dont tell me you are passing your root login details over an http request.....

    Create a new files called config.php in a directory that is not web accessible (ie, above the DOCUMENT_ROOT of your virtual host.

    In that file either create variables or defines for the mysql host, name, user & pass. require() the config file when you need it and everything will be 10000 times more secure (and likely to work).

    Christian

    Comments on this post

    • bigSeth agrees : Yay!
  14. #8
  15. No Profile Picture
    I AM A GOLDEN GOD
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Apr 2003
    Location
    Camarillo, California
    Posts
    5,929
    Rep Power
    1170
    Originally Posted by techker
    i have to be doing something wrong cause nothing works?
    Define "nothing works". When the form is POSTed, do any values get through to 'add2.php'? Does it show any error code?

    At the top of your 'add2.php' script, do this:
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors'1);

    // take a look at the POST data
    print_r($_POST);

    echo 
    'breakpoint 1';
    ...
    Continue adding breakpoints (and incrementing the value) in your script after each important statement.

    Also, make sure you echo out the value of your query. Store the query statement in a variable, let's call it $sql. That variable becomes the argument to [phpnet=mysql_query]mysql_query()[/phpnet].

    Plus, as the others have already mentioned, make sure you have a valid (preferably non-root) username and password to use in the script. DO NOT pass the MySQL user via a form, especially if it's the root user.

    Code:
    <input type="text" name="name" id="textfield" /> 
        name
    </p>
      <p>
        <input type="text" name="email" id="textfield2" />
      email</p>
      <p>
        <input type="text" name="job" id="textfield3" />
    Something else not right here; use the same name for the 'id' as you would in the 'name' value.

    Comments on this post

    • PranK agrees
    "Seriously, we're not a search engine, we're actual people." ~ ManiacDan

    BookMooch.com : Give books away. Get books you want.
  16. #9
  17. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Posts
    650
    Rep Power
    20
    ok so i added the brake point and modified my script now i get

    Array ( [name] => techker [email] => techker@gmail.com [job] => tech [button] => Submit ) breakpoint 1

    Comments on this post

    • bigSeth disagrees : I have no idea why people still help you.
    • PranK disagrees : Did you read *any* of the replies?

IMN logo majestic logo threadwatch logo seochat tools logo