Jump to content


Photo

putting HTML form $_POST['var'] in a function


  • Please log in to reply
7 replies to this topic

#1 j3rmain3

j3rmain3
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 30 October 2006 - 10:40 AM

i have a form which is used to rate documents. I have been able to create the form well, but now i am trying to get the rating into MySQL. the problem is for the rating to appear into the database, i need to use the $var = $_POST['var'];. I was trying to use the

if (mysql_fetch_row($results) > 0) {
	while ($row=mysql_fetch_rows($result)) {
		echo $row[1]tech = $_POST['$row[1]tech'];
	}
}

This was so when a new row was added to the table it would automatically add a new POST into the file. This would be alot easier for the user because they do not know how to code.

Help  ???

Thanks,
j3rmain3

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 30 October 2006 - 11:45 AM

WHat is the field name in your table?

In general the code you should use will look something like:
<?php
$q = "select * from tablename where fieldname = 'somevalue'";
$rs = mysql_query($q);
while ($row = mysql_fetch_assoc($rs)) {
    $qtmp = "update tablename set tech = '" . mysql_real_escape_string($_POST['tech'] . " where fieldname = 'somevalue'";
    $urs = mysql_query($qtmp) or die("Error updating database, query: $qtmp<br>" . mysql_error());
}
?>

Ken

#3 j3rmain3

j3rmain3
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 30 October 2006 - 11:53 AM

The fieldname is 'filename', but i havent labelled the field so when i type in $row['filename'] nothing appears, that is why i am using $row[1] in my coding.

j3rmain3

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 30 October 2006 - 12:01 PM

when i type in $row['filename'] nothing appears, that is why i am using $row[1] in my coding.


That's because mysql_fetch_row() only returns a numerical array, not an associative array.  If you wanted to use $row['filename'] then you'd need to use mysql_fetch_array() or mysql_fetch_assoc():

<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
   ...
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 j3rmain3

j3rmain3
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 30 October 2006 - 12:33 PM

Not sure if i explained my situation well because i am bit confused with the advice you peeps have given me. I wll use the mysql_fetch_assoc function but where is has

row[1]tech - the 'tech' bit is what i want to be added after the filename. filename is the name of row[1]. So it should produce something like this:

$filename1tech = $_POST['filename1tech'];
$filename2tech = $_POST['filename2tech'];
$filename3tech = $_POST['filename3tech'];

I am not sure if it has anything to with updating the database in mysql because it will not affect the data in there.

I want the file to be updated so that when the user enters a new row in the mysql table, the new filename will appear in the $_POST section as:

$filename4tech = $_POST['filename4tech'];

So the file has been updated without manually adding any coding.

Hope this has helped.

Thanks for the feedback so far........

J3rmain3

#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 30 October 2006 - 01:53 PM

I don't think that you've explained this well enough, and maybe you're getting confused with terminology.

My advice would be to leave all technical details out and post a description of what you're trying to achieve, something like this:

I have a web page with a form on it, the form fields are dynamically generated from the database, the form allows users to rate documents in the database.  I want the ratings applied in the form to be replicated through to the database.  Here's the code I have so far...

Something like that.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 j3rmain3

j3rmain3
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 30 October 2006 - 03:53 PM

Ok

I have created a site which allows users to view documents on a server and i am currently trying to create a rating system so people can rate each document.

I have created the database, the form and i am able to update the database perfectly through an HTML form. But what i am trying to do now is store the rating into mysql and get it saved. I am using dropdown menus as a way for the user to put in their rating.

This is the coding which i am currently structing to insert the rating. I have built some script so each row will appear with a drop down menu.


$connection = mysql_connect($host,$user,$pass) or die ("ERROR: Unable To Connect");

mysql_select_db($db) or die ("ERROR: Unable To Connect To DB");

$query = "SELECT * FROM url ORDER BY session";

$result = mysql_query($query) or die ("ERROR: Unable To Run Query".mysql_error());

if (mysql_num_rows($result) > 0) {

echo "<form name=ratings method=POST action=linkstable1.php>";
echo "<table border=1 cellpadding=3 cellspacing=3>";
echo "<tr>";
echo "<td><b><font face=Verdana size=-1>Session</td>";
echo "<td><b><font face=Verdana size=-1>Title Of Brainstorm Idea</b>";
echo "<td><b><font face=Verdana size=-1>Inventor Name</td>";
echo "<td><b><font face=Verdana size=-1>Rating</td>";
echo "</tr>";

while ($row=mysql_fetch_row($result)){
echo "<tr>";
echo "<td><font face=Verdana size=-1>".$row[4]."</td>";
echo "<td><font face=Verdana size=-1><a href=https://mysite.com/$row[2] target=_blank>".$row[1]."</a></td>";
echo "<td><font face=Verdana size=-1>".$row[3]."</td>";
echo "<td><select name=rates> <option value=  selected> <option value=1 name=$row[1]tech>1 - Poor <option value=2 name=$row[1]tech>2 - Fair <option value=3 name=$row[1]tech>3 - Average <option value=4 name=$row[1]tech>4 - Good <option value=5 name=$row[1]tech>5 - Excellent</select></td>";
echo "</tr>";
} 
echo "</table>";
} else { 
echo "No Rows Found!";
}
echo "<br>";
echo "<input type=submit value=submit name=sumbit_rating>";
echo "</form>";
?>


i have started a new php file which will display the new ratings.

I am confused about how to get the rating into the table and saving it.

When i was doing previous exercises, to get the data from an HTML form into a database, the processed php file needed to have

$var = $_POST['var']; at the top and this is for every input type which was being copied from the HTML form to the database.

My problem is i want the coding above to be updated every time new data has been added to the database, but i want it to be updated automatically, not manually.

So the scenario would be; a new row has been added to the database and then in the php file, a new line of code would be generated. So another one would appear when a new row was added to the database.

Hope i have explained it well.

Maybe if you undestand what i am trying to do with the coding here, you may understand what i mean alot better.


$connection = mysql_connect($host,$user,$pass) or die ("ERROR: Unable To Connect".mysql_error());

mysql_select_db($db) or die ("ERROR:Unable To Connect To DB".mysql_error());

$query = "SELECT * FROM url";

$result = mysql_query($result) or die ("ERROR:Unable To Connect Query".mysql_error());

if (mysql_fetch_row($results) > 0) {
	while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
		echo "$row[1]tech = $_POST[$row[1]tech]";
	} else {
		echo " ";
	}
}
		
?>

Thanks

#8 j3rmain3

j3rmain3
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 31 October 2006 - 10:45 AM

Sorry to bring this up again, but can someone please tell me what i am doing wrong because i still dont understand how to do it.

Thanks,
j3rmain3




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users