Jump to content

Please help... This code is not working


etdsbastar

Recommended Posts

hello there,

 

Please help.. This code is not working:

 

<?php
if(isset($_POST['btnsave'])=="Save")
{
	$name = $_POST['name'];
	$db = mysql_connect("localhost", $dbuser, $dbpass);
	if(!$db) die("Error connecting to MySQL database.");
	mysql_select_db("npms-2011", $db);
	$sql = "INSERT INTO test (name) VALUES (". $name .")";
	mysql_query($sql);
	exit();
}
?>

Link to comment
Share on other sites

No luck dear, any other solutions.

 

Here is my form code and button code:

 

<form name="wardnewform" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return ValidateFormWardsNew()">

 

<input type="submit" name="btnsave" value="Save" />

Link to comment
Share on other sites

You want to just display the posted content? like name?

just echo it out before you exit.

 

echo $name;

 

One more problem occured just now, only numbers are being stored in the table, If i enter some letter, it is not stored in the name field. Why is so?

 

And this is what happening when I am putting the echo command. The code is executing before the button press.

 

One more thing, On every page refresh a new record is being created in the table.

 

[attachment deleted by admin]

Link to comment
Share on other sites

Here is the complete code:

 

I think i am missing something. The following are the problems:

 

1. On every refresh a new record is being created.

2. Only numbers are being saved in the table record.

3. $echo is executed before the button press.

4. No luck for VARCHAR(255), instead I am using TEXT, still unable to save text entries in the field

 

Please help...

 

<link rel=stylesheet HREF="/css/wards.css" type="text/css" media="screen">
<?php include '../header.php'; ?>
<?php include '../config/config.php'; ?>
<?php include '../functions/functions.php'; ?>
<?php extract($GLOBALS); ?>

<div id="content">
	<div id="labelopt"><div class="box"></div> jax ds lHkh dkWye vfuok;Z gSa A  'kwU; ls Hkjs gq;s fdlh Hkh dkWye dks [kkyh ugha NksM+sa] vxj dksbZ izfo"Bh ugha gks rks 0 M+kysa A</div>
	<div id="labelopt"></div>
	<h2>u;s okMZ dk la;kstu</h2>
	<div id="dp-sp">
		<form name="wardnewform" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return ValidateFormWardsNew()">
			<table border="1">
				<tr>
					<td id="labelmust">okMZ dzekad %</td>
					<td><input class="english" type="text" size="20" name="wardno" maxlength="40" />
					<td id="labelmust">okMZ dk uke %</td>
					<td><input class="hindi" type="text" size="30" name="name" maxlength="30" />
					<td id="labelmust">t+ksu dzekad %</td>
					<td colspan="3"><input class="english" type="text" size="10" name="zone" maxlength="4" />
				</tr>
			</table>
			<table cellspacing="0" cellpadding="2">
				<tr>
					<th>vkoklh; ok"kZd HkkM+s dk C;ksjk</th>
					<th>fuekZ.k oxZ 01</th>
					<th>fuekZ.k oxZ 02</th>
					<th>fuekZ.k oxZ 03</th>
				</tr>
				<tr>
					<th id="thhead">eq[; lM+d ;k ckt+kj ij</th>
					<td align="center"><input class="english" type="text" size="10" name="a1" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="b1" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="c1" maxlength="5" value="0" />
				</tr>
				<tr>
					<th id="thhead">eq[; lM+d ;k ckt+kj ls gVdj</th>
					<td align="center"><input class="english" type="text" size="10" name="a2" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="b2" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="c2" maxlength="5" value="0" />
				</tr>
				<tr>
					<th>O;kolkf;d@vkS|ksfxd ok"kZd HkkM+s dk C;ksjk</th>
				</tr>
				<tr>
					<th id="thhead">eq[; lM+d ;k ckt+kj ij</th>
					<td align="center"><input class="english" type="text" size="10" name="a3" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="b3" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="c3" maxlength="5" value="0" />
				</tr>
				<tr>
					<th id="thhead">eq[; lM+d ;k ckt+kj ls gVdj</th>
					<td align="center"><input class="english" type="text" size="10" name="a4" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="b4" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="c4" maxlength="5" value="0" />
				</tr>
			</table>
		</div>
		<div id="menu">
		<table>
			<tr>
				<td>
					<input type="submit" name="btnsave" value="Save" />
		</form>
					<input type="button" value="View List" name="btnwardslist" onclick="location.href='/sp/spnew.php';" />
					<input type="button" value="Back" name="btnspback" onclick="location.href='/wards.php';" />
				</td>
			</tr>
		</table>
		</div>
	</div>
<?php include '../footer.php'; ?>

<?php
if(isset($_POST['btnsave']))
{
        $name = $_POST['name'];
        
	$db = mysql_connect("localhost", $dbuser, $dbpass);
	if(!$db) die("Error connecting to MySQL database.");
	mysql_select_db("npms-2011", $db);


	$sql = "INSERT INTO test (name) VALUES (". $name .")";
	mysql_query($sql);
	mysql_close($db);
	echo "successfully saved:".$name;
	exit();
}
else
{

}

/* $querys = "insert into wards (wardno, name, zone, a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4) values (wardno, name, zone, a1, a2, a3, b1, b2, b3, c1, c2, c4, d1, d2, d3, d4)"; */
?>

Link to comment
Share on other sites

I tested your code and it looks fine to me. what do you mean by "$echo is executed before the button press."?

and varchar(255) should work... could I see your table structure?

 

oh also, you need to clean your vars before inserting to db. mysql_real_escape_string()

Link to comment
Share on other sites

You cannot save any text because you don't have any single-quotes around the data in the query. Without the single-quotes, mysql is treating the data as a mysql keyword or column name. If you were checking if the query returned an error, you would have been getting sql syntax/unknown column name errors.

 

$sql = "INSERT INTO test (name) VALUES ('$name')";

Link to comment
Share on other sites

I tested your code and it looks fine to me. what do you mean by "$echo is executed before the button press."?

and varchar(255) should work... could I see your table structure?

 

oh also, you need to clean your vars before inserting to db. mysql_real_escape_string()

 

Thanks for the reply.

 

Here is my table's SQL:

 

CREATE TABLE  `npms-2011`.`test` (
  `name` varchar(255) CHARACTER SET utf8 NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1

 

Even with TEXT type it is not working.

 

Secondly, what is this mysql_real_escape_string(), how can i use it and where to put, please tell me clearly. Sorry for the problems I am a newbie.

Link to comment
Share on other sites

You cannot save any text because you don't have any single-quotes around the data in the query. Without the single-quotes, mysql is treating the data as a mysql keyword or column name. If you were checking if the query returned an error, you would have been getting sql syntax/unknown column name errors.

 

$sql = "INSERT INTO test (name) VALUES ('$name')";

 

Thanks a lot, you all guys helped me a lot. That solved my problem. I hope you will surely in future help me for all my queries.

 

Thanks a lot.

 

I will continue on this thread only if I will get some more errors regarding my current project.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.