Jump to content

Problem getting data into the database


derekbelcher

Recommended Posts

Hey there.  I have a website that has been "dumped" in my lap as my programmer has journeyed to greener pastures.  We have a small form that collects a little data and supposedly inserts it into the database, but for some reason that data never makes it there.  You can get the two pages that contain the code: http://dev.goshensparklingjewelry.com/page_1.txt is the page that the user goes to to insert the data, and http://dev.goshensparklingjewelry.com/page_2.txt is the magic page that makes it happen.  Not sure what is going on.  When you submit the data, you even get the success page as if it went through alright.  There is also a place in there that reads and displays data that is already in there, and that part works as well.

 

thanks!

Link to comment
Share on other sites

My apologies.  The links now work.  I forgot to upload the files.  I am also posting the code below.  The first code is for the page the user sees.  The second is the page that makes it all work.  I think.  What I am hoping is that someone with experience can take a look and say "This is why your data isn't making it to the databse"  I am not a programmer and really have no idea what I am looking at.  Just hoping for a quick fix.  Thanks

<?php 
	session_start();

	if (!ISSET($_SESSION["uid"])) {
		header( 'LOCATION OF THE WEBSITE HOME PAGE' ) ;
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Goshen Sparkling Jewelry - Princeton, WV  |  (304) 555-5555</title>
<link href="/css/goshenMain.css" rel="stylesheet" type="text/css" />
<link href="/css/wt-rotator.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">

function updateAlert()
{
var xmlhttp;
var alerttext = document.getElementById("alerttext").value;
var alerttype = document.getElementById("alerttype").value;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	document.getElementById("alertarea").innerHTML = xmlhttp.responseText;
    alert("Alert Successfully Updated.");
    }
  }
xmlhttp.open("POST","alertupdate.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("alert="+alerttext+"&alerttype="+alerttype);
}

function clearAlert() {
	document.getElementById("alerttext").value = '';
	document.getElementById("alerttype").value = 1;
	updateAlert();
}
</script>


</head>

<body>
<div class="container">
	<div class="navigation">
		<div style="float:left"><a href="?pid=0"><img src="images/template/Logo.gif" border="0" width="398" /></a></div>
		<div style="float:left"><img src="images/template/logo_right.gif" height="145" width="602" border="0" /></div>
	</div>

<div style="clear:both"></div>
		<!--Close Navigation-->
<div class="content">
	<div id="alertarea" class="mainText">
<?php
	$link = mysql_connect('localhost', 'USERNAME', 'PASSWORD');
	if (!$link) {
		die('Could not connect: ' . mysql_error());
	}
			
	mysql_select_db('DATABASE');

	//First we need to check for an existing demographic record.
	$alert_result = mysql_query("SELECT * FROM alerts LIMIT 1");
			
	while($row = mysql_fetch_array($alert_result))
	{
		$alert = $row["content"];
		$type = $row["type"];
	}
?>
	<table width="799" border="0" cellpadding="0" cellspacing="0" align="center" bgcolor="#ffffff">
		<tr>
			<td width="799" valign="top" style="background:#FFFFFF" align="center"><b>Promotion Alert Text</b><br></td>
		</tr>
		<tr>
			<td width="799" align="center" style="background:#FFFFFF" ></td>
		</tr>
		<tr>
			<td valign="middle" width="799" align="center" style="background:#FFFFFF" >
				<textarea name="alerttext" id="alerttext" cols="80" rows="5"><?php echo $alert; ?></textarea> 
			</td>
		</tr>
		<tr>
			<td valign="middle" width="799" align="center" style="background:#FFFFFF" > 
				
			</td>
		</tr>
		<tr>
			<td valign="middle" width="799" align="center" style="background:#FFFFFF" >
				Type of Promotion:  
				<select id="alerttype" name="alerttype">
				<?
					if ($type == 1) {
						print('<option selected value="1">% or $ off</option>');
					} else {
						print('<option value="1">% or $ off</option>');
					}
					if ($type == 2) {
						print('<option selected value="2">BOGO</option>');
					} else {								
						print('<option value="2">BOGO</option>');
					}
					if ($type == 3) {
						print('<option selected value="3">Shipping</option>');
					} else {
						print('<option value="3">Shipping</option>');
					}
				?>
				</select>
			</td>
		</tr>
		<tr>
			<td valign="middle" width="799" align="center" style="background:#FFFFFF" > 
				
			</td>
		</tr>		<tr>
			<td valign="middle" width="799" align="center" style="background:#FFFFFF" >
				<input type="button" name="updatebtn" id="updatebtn" value="Update Alert" onClick="updateAlert();">
				<input type="button" name="clearbtn" id="clearbtn" value="Clear Alert" onClick="clearAlert();">
			</td>
		</tr>
	</table>
    </div>
</div><!--Close Content-->
	
<div class="footer">
	<div style="float:left"><img src="images/template/footer.gif" height="81" width="435" border="0" alt="footer"></div>
	<div style="float:left"><a href="?pid=1"><img src="images/template/footer_build.gif" border="0" alt="Build Your Own" /></a><a href="?pid=2"><img src="images/template/footer_premade.gif" border="0" alt="Premade Items" /></a><a href="?pid=3"><img src="images/template/footer_special.gif" border="0" alt="Special Deals" /></a><a href="?pid=4"><img src="images/template/footer_sitemap.gif" border="0" alt="Sitemap and FAQ" /></a><a href="?pid=5"><img src="images/template/footer_contact.gif" alt="Contact Us" border="0" /></a><a href="?pid=6"><img src="images/template/footer_terms.gif" border="0" alt="Terms and Conditions" /></a></div><div style="float:right"><img src="images/template/footer_linksBottom.gif" border="0" /><a href="/login.php"><img src="images/template/login.gif" /></a></div>
    <div style="clear:both"></div>
	
	</div><!--Close Footer-->
</div><!--Close Main Container-->
</body>
</html>

Second page:

<?
	$alert = $_POST["alert"];
	$alerttype = $_POST["alerttype"];
	
	

	$link = mysql_connect('localhost', 'USERNAME', 'PASSWORD');
	if (!$link) {
		die('Could not connect: ' . mysql_error());
	}
			
	mysql_select_db('DB NAME');
	
	mysql_query("UPDATE alerts SET content = '".$alert."', type=".$alerttype." WHERE id = 1");	

	//First we need to check for an existing demographic record.
	$alert_result = mysql_query("SELECT * FROM alerts LIMIT 1");
			
	while($row = mysql_fetch_array($alert_result))
	{
		$alert = $row["content"];
		$type = $row["type"];
	}
?>
	<table width="799" border="0" cellpadding="0" cellspacing="0" align="center" bgcolor="#ffffff">
		<tr>
			<td width="799" valign="top" style="background:#FFFFFF" align="center"><b>Promotion Alert Text</b><br></td>
		</tr>
		<tr>
			<td width="799" align="center" style="background:#FFFFFF" ></td>
		</tr>
		<tr>
			<td valign="middle" width="799" align="center" style="background:#FFFFFF" >
				<textarea name="alerttext" id="alerttext" cols="80" rows="5"><?php echo $alert; ?></textarea> 
			</td>
		</tr>
		<tr>
			<td valign="middle" width="799" align="center" style="background:#FFFFFF" > 
				
			</td>
		</tr>
		<tr>
			<td valign="middle" width="799" align="center" style="background:#FFFFFF" >
				Type of Promotion:  
				<select id="alerttype" name="alerttype">
				<?
					if ($type == 1) {
						print('<option selected value="1">% or $ off</option>');
					} else {
						print('<option value="1">% or $ off</option>');
					}
					if ($type == 2) {
						print('<option selected value="2">BOGO</option>');
					} else {								
						print('<option value="2">BOGO</option>');
					}
					if ($type == 3) {
						print('<option selected value="3">Shipping</option>');
					} else {
						print('<option value="3">Shipping</option>');
					}
				?>
				</select>
			</td>
		</tr>
		<tr>
			<td valign="middle" width="799" align="center" style="background:#FFFFFF" > 
				
			</td>
		</tr>		<tr>
			<td valign="middle" width="799" align="center" style="background:#FFFFFF" >
				<input type="button" name="updatebtn" id="updatebtn" value="Update Alert" onClick="updateAlert();">
				<input type="button" name="clearbtn" id="clearbtn" value="Clear Alert" onClick="clearAlert();">
			</td>
		</tr>
	</table>
Link to comment
Share on other sites

I don't see anything overtly wrong with the code, however, the update is always changing the row in the alert table with id = 1.

 

The code that queries the alert table and displays it, does not select WHERE id = 1, but rather does a LIMIT 1 (limiting whatever the result set is, to only return one record). This could be the source of a subtle bug.

 

Check your alert table with command line mysql or phpmyadmin. There might be more than one row in there, and could explain why you are seeing one row come up, even though you are updating the form with different values. The row that is being returned may not be the row that is getting updated.

 

If this is the problem, to fix this code, change the SELECT queries in both scripts from:

 

$... = mysql_query("SELECT * FROM alerts LIMIT 1");
to

 

$... = mysql_query("SELECT * FROM alerts WHERE id = 1");
If this is not the problem, you would really need to put in debugging statements into the code to figure out what is going on.
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.