Jump to content

question about variables


gevo12321

Recommended Posts

ok so i have a page with an input on it:

echo "<input type=\"text\" name=\"button";
echo $id;
echo "\" value=\"\"/>

 

ok and the page also has a submit button when u press it its supposed to put the text that u typed in in the input into a database in the row corresponding with the id number $id

 

now in the other php page, the one that does the action,

 

how would i post it since its a variable:

$name=$_POST[what goes in here?];
mysql_query("UPDATE `layout` SET `name` = '". $name . "',
WHERE id = '" . $id . "'");

would i write button$id or something?

 

can someone plz help me with this?

 

thank you

Link to comment
Share on other sites

Edit : Whoops, i didn't read your post properly.

 

If, as i would guess, you have a number of these form elements that you wanting to put into a database, you can use arrays. An example might be:

 

<input type="text" name="name[0]" />
<input type="text" name="name[1]" />
<input type="text" name="name[2]" />

 

You can then use a loop on the array:

<?php
$name= $_POST['name'];
foreach($name as $value){
//insert the name into the database etc
}

?>

 

 

Link to comment
Share on other sites

i think u didnt understand my first question

 

i was wondering if there is any way of doing it the way i suggested in my first post and not using arrays

 

and as for my code, here it is:

 

this is the form

<?php
require_once('../connect.php');

$query = mysql_query("SELECT * FROM layout") or die(mysql_error());

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<title>E Layout</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
</head>
<body>
<a href=\"eenglish.htm\">English<a> > <a href=\"eeditlayout.php\">E Layout<a>
<hr>
<form action=\"esubmiteditlayout.php\" method=\"post\">
<table width=\"100%\"  border=\"0\">";
while($row=mysql_fetch_object($query))
{
$id=$row->id;
$name=$row->name;
$source=$row->source;
echo "
  <tr>
    <td valign=\"top\" width=\"2%\"><input type=\"checkbox\" name=\"checkbox";
echo $id;
echo "\"></td>
    <td valign=\"top\" width=\"5%\">Button ";
echo $id; 
echo ":</td>
    <td valign=\"top\" width=\"10%\"><input type=\"text\" name=\"button";
echo $id;
echo "\" value=\"";
echo $name;
echo "\"/></td>
    <td valign=\"top\" width=\"8%\">Button ";
echo $id;
echo " Source:</td>
    <td valign=\"top\" align=\"left\"><input type=\"text\" name=\"buttonsrc";
echo $id;
echo "\" value=\"";
echo $source;
echo "\"/><input type=\"hidden\" name=\"";
echo $id
echo "\"></td>
  </tr>";
}
echo "
</table>
<input type=\"submit\" name=\"edit\" value=\"Edit\">
<input type=\"submit\" name=\"insert\" value=\"Insert\">
<input type=\"submit\" name=\"del\" value=\"Delete\" onclick=\"return deldel()\">
<script language=\"javascript\">
function deldel()
{
   var agree=confirm(\"Are you sure you want to delete the checked buttons from the Side Bar?\");
   if(agree)
      return true;
   else
      return false;
}
</script>
</form>
</body>
</html>";

mysql_close($link);
?>

 

here is the submit

<?php
require_once('../connect.php');

$edit=$_POST[edit];
$insert=$_POST[insert];
$delete=$_POST[del];
$name=$_POST[];
$source=$_POST[];

if (isset($delete))
{
}
else if (isset($insert))
{
mysql_query("INSERT INTO layout (id, name, source)
VALUES ('', '', '')");

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<meta http-equiv=\"Refresh\" content=\"1; url=eeditlayout.php\"/>
</head>
<body>
New Button has been Inserted.<br>
Thank You
</body>
</html>
";
}
else if (isset($edit))
{
  while($row=mysql_fetch_object($query))
  {
  $id=$row->id;
  mysql_query("UPDATE `layout` SET `name` = '". $name . "', `source` = '".$source."',
WHERE id = '" . $id . "'");
  }
}
mysql_close($link)
?>

 

i really appreciate ur help

 

thank you

Link to comment
Share on other sites

Well, first off, i suggest you try and learn a little about arrays. There's an introduction to them here:

http://www.phpfreaks.com/tutorials/42/0.php

 

Edit: plus there's a section from a free online php book about arrays here:

http://hudzilla.org/phpwiki/index.php?title=Arrays

 

Try reading through those first and see if you can understand the way this might work.

Link to comment
Share on other sites

ok

 

i understand what it is now but i just dont see the difference between just defining a variable and defining an array. its just as hard. i mean i can define 2 variables as

$variable1="HI";

$variable2="BYE";

 

but to define the same thing as an array, i would have to do

 

$variable=array("HI","BYE");

 

i mean i guess its a little easier to write since i don't have to repeat the same thing all over again but to echo the array i would have to go through the same process as with a variable

 

instead of

 

echo $variable1;

 

i would have to

 

echo $variable[1];

 

so I'm still faced with the problem i was faced with earlier with the variables.

 

how would i define the array because the number of elements is not constant

 

and how would i be able to implement this into the submit script.

Link to comment
Share on other sites

I felt the easiest way to answer your questions was to create a working example. If you set up a table(called users in my example) with two fields , id and username. Populate it with a few bits of data.

 

Or you can import this using phpMyAdmin if its easier:

CREATE TABLE `users` (
  `id` int(20) NOT NULL auto_increment,
  `username` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

-- 
-- Dumping data for table `users`
-- 

INSERT INTO `users` (`id`, `username`) VALUES (1, 'tom'),
(2, 'dick'),
(3, 'harry'),
(4, 'jim'),
(5, 'bob'),
(6, 'bill');

 

Then, the script:

<html>
<head>
</head>
<body>
<?php
include("test_db_connection.php");
if(isset($_POST['submit'])){//form has been submitted, so process it
if(count($_POST['edituser']) > 0){//first check to see if we are trying to edit anyone. otherwise we get an error
	foreach($_POST['edituser'] as $key => $value){
		//as the index of each element of the array, $key will contain the user ID
		$new_username = $_POST[username][$key];
		mysql_query("UPDATE `users` SET `username`='$new_username' WHERE `id`=$key") or die(mysql_error());
		echo "User $key# has changed their name to $new_username <br />";
	}
}
if(count($_POST['deleteuser']) > 0){//first check to see if we are trying to delete anyone. otherwise we get an error
	foreach($_POST['deleteuser'] as $key => $value){
		mysql_query("DELETE FROM `users` WHERE `id` ='$key'") or die(mysql_error());
		echo "User $key# has been deleted<br />"; 
	}
}
}
?>
<form action="phpfreaks.php" method="post">
<table border="1">
<tr><td>User ID# </td><td>Username</td><td>Edit?</td><td>Delete?</td></tr>
<?php
$sql = mysql_query("SELECT * FROM `users`") or die(mysql_error());//grab all our users from the table
while($row = mysql_fetch_assoc($sql)){//cycle through each user
echo "<tr><td>$row[id]</td><td><input type='text' name='username[$row[id]]' value = '$row[username]' /></td><td><input type='checkbox' name='edituser[$row[id]]' /></td><td><input type='checkbox' name='deleteuser[$row[id]]' /></td></tr>";
//notice how i use the id as the index for all the arrays. This is because this is the the one thing that uniquely identifies each person
}
?>
<tr><td colspan="4"><input type="submit" name="submit" value="Edit/Delete" /></td></tr>
</table>
</form>
</form>
</body>
</html>

 

Its just a quick piece of code to show you how it all works. But its tested and does work. Play around with it , i hope it will answer your questions. If there's any of the code you dont understand, feel free to ask.

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.