Adding item to database

Im trying to add the same item to a database, but with a differnt ID

it does add a new item with a different id, but its blank, i want it to duplicate the item with a different id

Getting


<?php
$q = "SELECT name, id FROM website0 ORDER BY name ASC";
?>

Insert



<?php
require_once ('mysqli_connect.php');

 $text = mysqli_real_escape_string($dbc, $_POST['name']);  
 $sql = "INSERT INTO website0 SET
     URL='$text'";  
 if (!mysqli_query($dbc, $sql))  

 exit(); 
 
 header("location:view_users2.php");
 
 exit();  


?>

got it



<?php
require_once ('mysqli_connect.php');

 $text = mysqli_real_escape_string($dbc, $_POST['id']);  
 $sql = 'INSERT INTO website0 SET
     COLUMN1="'.$text.'"';  
 if (!mysqli_query($dbc, $sql))  

 exit(); 
 
 header("location:view_users2.php");
 
 exit();  


?>

table is

ID - COLUMN1
1 words
2 words2

when i run the code with id it does this

ID - COLUMN1
1 words
2 words2
3 2 <---- new

if i try to replace ‘id’ with ‘Column1’ and run

ID - COLUMN1
1 words
2 words2
3 2
4 <----new but column1 is empty

Paste your table layout and your code you are using.

my table is good, its when i input data into it using my code below i cant seem to get ‘name’ inputted into the column url

The easiest way to do this would be to set the id column as AUTO INCREMENT. :slight_smile:


CREATE TABLE `database`.`table` (
  `id`      MEDIUMINT       NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `website` VARCHAR( 45 )   NOT NULL ,
  `name`    VARCHAR( 45 )   NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;

Then make the id field auto-increment itself, by defining that behaviour when you set the table up or you can use a modify command, but back your table up first - as ever.

mytable

id - INT auto-increment
URL - VARCHAR

then you can drop the need to insert the ID, just use:

“insert into mytable (URL) values ‘$text’”

or

“insert into mytable (id, URL) values 0, ‘$text’”

or

“insert into mytable values 0, ‘$text’”

and mysql will increment the id for you automatically.

hmm it says my index: name is undefined

but when i replace with id, it sticks the id into the name column,

i want name in the name column

Did you end up fixing the query>

Are you sure your ID field is auto increase? I would use the first query Cups suggested. The query in your first post is not correct hence why it wont insert.