insert problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
zuzupus
Forum Commoner
Posts: 70
Joined: Thu Jul 17, 2003 4:54 am

insert problem

Post by zuzupus »

hi,

i created a table

CREATE TABLE emp_details (
id int(10) NOT NULL auto_increment,
projektnr varchar(100) NOT NULL default '',
hvorgang varchar(100) NOT NULL default '',
uvorgang varchar(100) NOT NULL default '',
soll varchar(100) NOT NULL default '',
status varchar(100) NOT NULL default '',
PRIMARY KEY (id)
) TYPE=MyISAM

now the problem is im trying to insert the values for hvorgang and uvorgang for particular id thru application for that i created a text field and submit button im using php4.0.6

Code: Select all

$nr +=0;
 if (isset($HTTP_POST_VARS['add'])){
 mysql_query("INSERT into emp_details SET hvorgang = '".$HTTP_POST_VARS['newhvorgang']."' WHERE id = $nr");
 mysql_query("INSERT into emp_details SET uvorgang = '".$HTTP_POST_VARS['newuvorgang']."' WHERE id = $nr");
   }
but im not able to get iserted teh value in database anybody help me out be appreciable
thanks in advance
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

Try

Code: Select all

$query = mysql_query("INSERT into emp_details SET hvorgang = '".$HTTP_POST_VARS['newhvorgang']."' WHERE id = $nr"); 

die($query);
That way you'll be able to see exactly what's being sent to MySQL, and may notice where the error is. My guess? It looks like you've got an unnecessary set of single quotes in there.
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

removed
Last edited by JAM on Fri Sep 05, 2003 8:45 am, edited 1 time in total.
dekemp
Forum Newbie
Posts: 3
Joined: Fri Sep 05, 2003 5:41 am
Location: The Netherlands

Post by dekemp »

Oops!
Syntax error! and Semantic error as well.

I I read the original message he is trying to add a value to hvorgang in a particular record with id = nr.
I could be wrong but INSERT ?? should this not be UPDATE ??
From the text and espacially if two insert follow each other, he is adding 1 record for every hvorgang and uvorgang and suppliying the id which is an automatic_increment primary key.
The errors are 'duplicate record'.
The real error is using a INSERT in stead of UPDATE

Bitte brauchen Sie UPDATE und nicht INSERT. Insert macht eine neue reihe im tabel un dass gibt 'duplicate key error'

Dekemp
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

dekemp are very right.
I was basing my comment as an alternative syntaxing from Unipus's example and reading "insert" in the original post.

Code: Select all

$nr +=0; 
if (isset($HTTP_POST_VARSї'add'])) { 
    mysql_query("UPDATE emp_details SET hvorgang = '$HTTP_POST_VARSїnewhvorgang]', uvorgang = '$HTTP_POST_VARSїnewuvorgang]' WHERE id = '$nr'"); 
}
zuzupus
Forum Commoner
Posts: 70
Joined: Thu Jul 17, 2003 4:54 am

Post by zuzupus »

thanks alot all of u but i can use INSERT---SET

plz have a look at http://www.mysql.com/doc/en/INSERT.html
now it works fine actually i modified but anybody plz help me how to update the selected value from drop down let say if i select test and it will display in text field and then when i change it to any name let say FORUM then this name be replaced hope this is clear

plz have a look at this link http://server2.vitodesign.com/scripts/drop.phtml for more clearity

i want update to work rest is working add and delete

Code: Select all

<?
         include("../settings.php");
             /* Connecting, selecting database */
             $link = mysql_connect("$dbhost", "$dbuser", "$dbpw")
             or die("Could not connect");

             mysql_select_db("$dbuser") or die("Could not select database");

#ADD ITEM
	if (isset($HTTP_POST_VARS["add"]))
	{
		if (isset($HTTP_POST_VARS["NEWuvorgang"]) && $HTTP_POST_VARS["NEWuvorgang"]!="")
		{
			mysql_query("INSERT INTO uvorgang SET name = '".$HTTP_POST_VARS["NEWuvorgang"]."'");
		}
		elseif (isset($HTTP_POST_VARS["NEWhvorgang"]) && $HTTP_POST_VARS["NEWhvorgang"]!="")
		{
			mysql_query("INSERT INTO hvorgang SET name = '".$HTTP_POST_VARS["NEWhvorgang"]."'");
		}
	}
#REMOVE ITEM
	if (isset($HTTP_POST_VARS["remove"]))
	{
		if (isset($HTTP_POST_VARS["uvorgang"]))
		{
			mysql_query("DELETE FROM uvorgang WHERE name = '".$HTTP_POST_VARS["uvorgang"]."'");
		}
		elseif (isset($HTTP_POST_VARS["hvorgang"]))
		{
			mysql_query("DELETE FROM hvorgang WHERE name = '".$HTTP_POST_VARS["hvorgang"]."'");
		}
	}

?>

<TABLE border="1" width="170" height="1" bgcolor="#666666" align="left" cellspacing="0" cellpadding="0" bordercolor="#FFFFFF">
                	 		<TR bordercolor="#FFFFFF" bgcolor="#333333">
	                           <TD height="1" colspan="3">
	                             <P align="center"><B><FONT face="Arial" size="2" color="#FF6600">Hauptvorgang</FONT></P>
	                           </TD>
	                         </TR>

                	 		<TR bordercolor="#FF6600">
                                    <TD nowrap>Enter Option Name:
                                    <?
                                    $queryH = mysql_query("SELECT * FROM hvorgang");
         	                        echo "  <form name=FORMhvorgang action='' method=post>
	                                                    <input type=text size=10 name=NEWhvorgang>
	                                                    <SELECT name=hvorgang>";
	                                    while($hvorgang=mysql_fetch_object($queryH))
	                                    {
	                                            echo "<option>$hvorgang->name</option>";
	                                    }
	                                    echo "  </SELECT>&nbsp;<input type=submit name=add value=add>
	                                                    <input type=submit name=remove value=remove></form>";
                                    ?>
                                    </TD>
                                  </TR>

                                  <TR bordercolor="#FFFFFF" bgcolor="#333333">
	                           <TD height="1" colspan="3">
	                             <P align="center"><B><FONT face="Arial" size="2" color="#FF6600">Untervorgang</FONT></P>
	                           </TD>
	                         </TR>

                                 <TR bordercolor="#FF6600">
                                    <TD nowrap>Enter Option Name:
                                    <?
                                    $queryU = mysql_query("SELECT * FROM uvorgang");
         	                        echo "  <form name=FORMuvorgang action='' method=post>
	                                                    <input type=text size=10 name=NEWuvorgang>
	                                                    <SELECT name=uvorgang>";
	                                    while($uvorgang=mysql_fetch_object($queryU))
	                                    {
	                                            echo "<option>$uvorgang->name</option>";
	                                    }
	                                    echo "  </SELECT>&nbsp;<input type=submit name=add value=add>
	                                                    <input type=submit name=remove value=remove></form>";
                                    ?>
                                    </TD>
                                  </TR>


              </TABLE>
Post Reply