how to insert different data with same id.

i have a table tech sub with two attributes “tusername and sub_code”.
i get tusername from previous page using request.
and this page i have a form with subject 1, subject 2.
i want to add sub_code to one tusername.
how can i do that.
techsub.php
codes:

	 [php]<?php

include(“db.php”);
$tusername =$_REQUEST[‘tusername’];

@mysql_query(“INSERT INTO techsub(sub_code, tusername)
VALUES (’$sub_code’,’$tusername’) where tusername=’$tusername’”);
?>

Subject One:
Subject Two:
Subject Three:
Subject Four:
<?php '?sub_code=$sub_code' ?> [/php]

Not quite sure i understand what you want to do but there are however many issues in your code.

[php]<?php
@ // DON’T DO THIS - This suppresses errors!
mysql_query // DON’T USE THIS - This was deprecated over 10 years ago, use mysqli or PDO instead!
[/php]

You do realise naming these all the same will result in whatever is in the last one being the final value?
IE: two will overwrite one, three will overwrite two, etc. etc.

Subject One: <input type="text" name="sub_code">
Subject Two: <input type="text" name="sub_code">
Subject Three: <input type="text" name="sub_code">
Subject Four: <input type="text" name="sub_code">

Hope that helps point you in the right direction,
Red :wink:

You are trying to update a row in the table using INSERT which won’t work.
Try this…

UPDATE `techsub` 
SET 
	sub_code = '$subcode', 
	tusername = '$tusername' 
WHERE tusername = '$username'

Keep in mind if sub_code is an integer don’t wrap in quotes

UPDATE `techsub` 
SET 
	sub_code = $subcode, 
	tusername = '$tusername' 
WHERE tusername = '$username'

Also this method is prone to injection

** Just add if you are not changing the username don’t add it to the query…

UPDATE `techsub` 
SET 
	sub_code = $subcode
WHERE tusername = '$username'

[member=73602]polk_farody[/member], your are right that as posted, the Insert will not work, but I would like to point out if the database was setup correctly you could do an INSERT … ON DUPLICATE KEY UPDATE query. Probably not what the OP needs though.

For those not familiar with it, you can read up on it in the Mysql Docs here:
http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html

^^^
Above is one way, you could also learn prepared statements, much better. 8)

[php]
// Your query should look something like this…
$query = UPDATE techsub
SET
sub_code = ?,
tusername = ?
WHERE tusername = ?

if($mysqli->prepare($query)) {
$mysqli->bind_param(‘iss’, $subcode, $username, $username);

etc.
etc.

[/php]

My two-pence.
Red :wink:

PS: Don’t copy and paste this code because it is incomplete, just shown as an example.

Sponsor our Newsletter | Privacy Policy | Terms of Service