0

i have a table in mysql with 2 fields (num and name)

i need to insert lots of names and each name has one number like the example below

i need php in "page 2" to insert next number after the last one (in this case, number 5) if i insert only the name and leave the number txt empty, in other words, i need it to be with default text inside, but this text is the last number in the table (in this case, number 5).

1 - john
2 - maria
3 - monica
4 - george

page 1 form_page.html

<form action="add.php" method="post">
    <input type="text" name="txt_num">
    <input type="text" name="txt_name">
    <input type="submit">
</form>

page 2 add.php

<?php
include('connect_db.php');  
                $sql="INSERT INTO(
                    num,
                    name,
                )
                VALUES(
                    '$_POST[txt_num]',
                    '$_POST[txt_name]',
                )";
?>

page 3 connect_db.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database_name";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("<p2>Database connection failed: </p2>" . mysqli_connect_error());
}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    First you Insert query will not work because you are missing the table name after INSERT INTO. Now, why would you insert $_POST[txt_num]? why not make that num column integer with auto_increment ? – Jaylen Nov 12 '14 at 19:39
  • 1
    You have extra commas in your query and missing your table name to insert "into", not to mention that you're not querying, so I call this a *fill in the blanks* question. – Funk Forty Niner Nov 12 '14 at 19:45
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Nov 12 '14 at 20:01

2 Answers2

0

Your solution my be Auto_Increment which will do the work for you.

You will need to make your num auto_increment and make is a primary key of that table.

connect to MySQL and try the following queries:

ALTER TABLE tbl_name ADD COLUMN `id` int(11) unsigned primary KEY AUTO_INCREMENT;
ALTER TABLE tbl_name DROP COLUMN num;
ALTER TABLE tbl_name CHANGE id num int(11) unsigned;

On a side note your code above will not work because you are missing table name and have extra commas.

assuming you alter the tables like I suggest your query will look like this

        $sql="INSERT INTO tbl_name(name)VALUES($_POST[txt_name])";

Note: that you would need to escape the txt_name value otherwise your code is vulnerable to attacks.

Jaylen
  • 39,043
  • 40
  • 128
  • 221
0

You can:

  • Set "AUTO_INCREMENT" on "num" in MySQL.
  • Select last "num" and Insert.

Exemple (select last and insert if you not post 'num'):

//if 'num' is null
if($_POST['txt_num'] == '' or !isset($_POST['txt_num']){ 

        $LastNum = "SELECT num FROM table_name ORDER BY num DESC LIMIT 1";
        $LastNum = mysql_fetch_array($LastNum);
        $Num = $LastNum['num'] +1; 

//if you set value    
}else{                                                
        $Num = $_POST[txt_num]; 
}


    $sql = "INSERT INTO table_name(num,name) VALUES('".$Num."','".$_POST['txt_name']."')";
Inkeliz
  • 892
  • 1
  • 13
  • 25