We help IT Professionals succeed at work.
Get Started

Update Data in a Database by PHP

299 Views
Last Modified: 2012-04-23
This is what it is on a Database but a location that read: Aungier Street, 4037 on a first row of a database and it is incorrect. It should read as Aungier Street, 4031.

[u][b]
moduleNo 	moduleName 	dueDate 	        location[/b][/u]
999001 	        Dynamic Web  	2012-05-14 	Aungier Street, 4037
999006 	        Digital Audio 	  	2012-05-10 	Aungier Street, 3025

Open in new window



I've corrected it as below and followed the instructions with a link: http://www.w3schools.com/php/php_mysql_update.asp it did not work as expected.
mysql_query("UPDATE moduleTable SET location=Aungier Street, 4031
WHERE dueDate='2012-05-14'");

This is PHP as below
<?php
$q=$_GET["q"];

// 1 create a database connection
$conn = mysql_connect('localhost', 'root', 'root') or die ('Error connecting to mysql');

// 2 select a database to use
$databaseName = "collegeData"; 
$tableName = "moduleTable";	
$conn = mysql_select_db($databaseName) or die( "Unable to select database");

//$sql="SELECT * FROM moduleTable WHERE moduleName= '".$q."'"; //change FROM user to studentTable
$sql="SELECT * FROM moduleTable WHERE moduleName= '".$q."'"; //change FROM user to studentTable
mysql_query("UPDATE moduleTable SET location=Aungier Street, 4031
WHERE dueDate='2012-05-14'");

//$query="SELECT * FROM studentTable ORDER BY moduleNo1 DESC LIMIT 10 ";
//<table style="width100%">

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
  {
	echo "<hr/><table><tr><td>Start Date:</td><td>" . $row['dueDate'] . "</td></tr></table><hr/>
    <table><tr><td>Location:</td><td>" . $row['location'] . "</td></tr></table><hr/>";
  }

mysql_close($con);
?> 

Open in new window


It may not be necessary to add this as below. For your information as below.

HTML
<script type="text/javascript" src="js/getusermodule.js.js"></script>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a module:</option>
<option value="Production Skills">Production Skills</option>
<option value="Digital Audio">Digital Audio</option>
</select>
</form>
<br />
<div id="getmodule">Starting date will be listed here.</div>

Open in new window


JS
function showUser(str)
{
if (str=="")
  {
  document.getElementById("getmodule").innerHTML="";
  return;
  }
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("getmodule").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","php/getusermodule.php?q="+str,true);
xmlhttp.send();
}

Open in new window

Comment
Watch Question
Freelancer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE