Jump to content
Sign in to follow this  
SpeedCrazy

PHP MYSQL INSERT error?

Recommended Posts

something that is not related to it working.

Naming scheme: I noticed you used camel case (totalProfit has a capital P) and all lower case (soldto, numsold). I for one like consistency to make life easier.

 

If you're struggling to get stuff to work throw in some print statements. So get the result from the query and do:

if($result){echo "true";}
else{echo "false";}

 

Another thing to do is to remove things until you get it to work. Then add stuff back and you can find out what is broken. So I took your insert statement and reduced it to

$result = mysql_query ("INSERT INTO income (numsold) VALUES ($num)");

which worked. So that tells me your syntax is correct. I started adding values back and it broke on me at "soldto". I didn't think about it being a longtext type and so I had the wrong type of input. You might check your input for that one. I didn't have all of you $_REQUEST variables so I cheated and just hard coded the values so mine might be messed up. Speaking of longtext, do you really need longtext instead of varchar? or even text?

 

Anyways, I have to go to work now. That is how I debug things though so you might start there.

Share this post


Link to post
Share on other sites

I will switch it, though i took the insert statement and put it in in myphpadmin and it worked, so i think it has something to do with my php.

Share this post


Link to post
Share on other sites

Still broken.... i think i might have to start over. :cry:

Share this post


Link to post
Share on other sites

Still broken.... i think i might have to start over. :cry:

I'll help you but i'll need all the files. I had to cut out a lot because I didn't have the request variables. Is there a reason your were using longtext and not a varchar for that one variable? If you still want help pm me.

Share this post


Link to post
Share on other sites

HTML for it.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtm11/DTD/xhtml11-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Business Accounts</title>
</head>
<body>
	<form action="bACC.php"
				method="post">
		<fieldset>
			<label>Sales Info</label>
			<fieldset>
				<label>Template 1</label>
				<input type="checkbox" name="templateNum" value="1"/><br />
				<label># Sold:</label>
				<input type="text" name="template1S" size="4"/>
				<label>Sold To:</label>
				<input type="text" name="template1To"/>
			</fieldset>

			<fieldset>
				<label>Template 2</label>
				<input type="checkbox" name="templateNum" value="2"/><br />
				<label># Sold:</label>
				<input type="text" name="template2S" size="4"/>
				<label>Sold To:</label>
				<input type="text" name="template2To"/>
			</fieldset>

			<button type="submit">submit</button>
		</fieldset>				
	</form>
</body>
</html>

Share this post


Link to post
Share on other sites

The problem is with the longtext type. I don't remember off the top of my head how to insert data into a longtext field. Is there a reason you're not using a varchar type? That field isn't going to be very long since it's just someones name. I have to go to work but I'll see what I can come up with after I get off. Here is something else to think about though.

 

I think you have some extra code that makes this more difficult than it needs to be. Lets look at template number for example.

 

you store the value from the $_REQUEST variable to the variable $templateNum

then you check the number and then depending on the value you store templateNums (a separate variable) as either 1 or 2 which is the same value as $templateNum.

then you store $templateNums in $tp.

so you go through

$templateNum = $_REQUEST["templateNum"];
if($templateNum ==1) {
  $templateNums = "1";
} else{
  $templateNums = "2";
}//end if
$tp= "$templateNums";

You could save yourself some time and confusion by just doing

$tp = $_REQUEST["templateNum"];

Share this post


Link to post
Share on other sites

but it changes the other variables, it means that if info was typed into the fields in the second set that it would be ignored. Your way you could get multiple data sets conflicting.

if($templateNum ==1) {
        $templateNums = "1";
        $templateS = "$template1S";
        $templateTo = "$template1To";
        } else{
            $templateNums = "2";
            $templateS = "$template2S";
            $templateTo = "$template2To";
}//end if

This way if there is data in both initial variables the one not marked with the check box is ignored.

Share this post


Link to post
Share on other sites

but it changes the other variables, it means that if info was typed into the fields in the second set that it would be ignored. Your way you could get multiple data sets conflicting.

if($templateNum ==1) {
        $templateNums = "1";
        $templateS = "$template1S";
        $templateTo = "$template1To";
        } else{
            $templateNums = "2";
            $templateS = "$template2S";
            $templateTo = "$template2To";
}//end if

This way if there is data in both initial variables the one not marked with the check box is ignored.

you could still cut out a couple of assignments though.

Share this post


Link to post
Share on other sites

I feel like a noob that I didn't see this at first. You need to add quotes around the $to.

mysql_query ("INSERT INTO income (date, templateID, numsold, moneymade, totalProfit, soldto) VALUES (NOW(), $tp, $num, $mmS, $mm, '$to')");

 

Just goes to show that if you step away from it for a while and take a break you'll likely see what's wrong when you get back.

Edited by flareback

Share this post


Link to post
Share on other sites

Why dont the other variables need those quotes?

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Sign in to follow this  

×
×
  • Create New...