Jump to content

PHP Code Help Plase!!!!


soulsociety

Recommended Posts

hey everyone!

 

I Need an update data code from a form.

 

First of all I need a list which shows all the rows from my database. then when user select the row which he/she want to edit, he/she can be able to fill the form and when enters the update button that row should be updated.

 

Can anyone help me how to do such thing.

 

I would really appreciate it.

Link to comment
Share on other sites

Help US help you!!! :)

 

Need a little more info. Do you know how to query the database?? do you know how to loop through the results and make a list?? do you know how to create forms for update?? I mean we can't build the site for you here so some you will have to create. Where are you stuck??

 

Ray

Link to comment
Share on other sites

thank you for your reply.

 

Yes I know how to query database and I created a from. I also can show the rows of table in a select item. What  I cant do is I want my list box dynamically show all the items in it. I only achieved to call a query and show the rows manually. I mean when a new data is inserted I have to cal another query.

 

The other think Can I make such a form that when I select a row from the list and it shows me the data in the form and I can edit it?

 

Please if you can help I would appreciate it

 

Link to comment
Share on other sites

To be more Specific

 

<?php

class SomeClass {

public $insert_id=0;

private $host="localhost";

private $User="ciftlikt_lab";

private $Pass="test@lab";

private $DB="ciftlikt_deneme";

private $connection;



private function SC_connect_db(){

  $this->connection=mysql_connect($this->host ,$this->User,$this->Pass  );

  if (!$this->connection) die('No Connection');

  

  $res=mysql_select_db ($this->DB ,  $this->connection);

  if(!$res) die("No Database");

  return $res;

}



public function query($sql){

$this->SC_connect_db();

return mysql_query  ($sql ,$this->connection );

}
  


public function get_results($sql){

  $result= $this->query($sql);

  

  while ($row = mysql_fetch_assoc($result)) {

  $results[]=(object)$row;

  }

  return $results;

  

}

public function add_user($user) {

$result= $this->query($sql);

$sql ="INSERT INTO deneme (NULL, '$user->id', '$user->name')";



}

public function insert($table, $var){

$sql="INSERT INTO $table VALUES (";

foreach ($var as $va){

$sql.="'$va',";

}



$sql.= ")"; 

$sql=str_replace( ",)" ,  ")" , $sql );



  $result= $this->query($sql);

$this->insert_id= mysql_insert_id ($this->connection);

return  $result;

}


}


$myDBClass = new SomeClass;


$sql1 = "CREATE TABLE Assignment
(
Name varchar(15),
Surname varchar(15),
ID int,
AssignmentNumber int
)";
$myDBClass ->query($sql1);

// add fields

if (isset($_POST['submit1'])) {



$formarray = array(($_POST['ad']), ($_POST['soyad']), ($_POST['id']), ($_POST['assignment']) );

$myDBClass->insert("Assignment" , $formarray);

}




if (isset($_POST['Update'])) {

$name = $_POST['ad2'];
$surname = $_POST['soyad2'];
$assignment = $_POST['assignment2'];
$id = $_POST['id2'];
$sql2 = "UPDATE Assignment
SET Name = '$name', assignmentNumber = '$assignment', Surname='$surname'
WHERE ID = '$id'";

$myDBClass ->query($sql2);

}







$sql3 = "DELETE FROM Assignment WHERE ID=0 ";
$myDBClass ->query($sql3);




new dbug($myDBClass ->get_results("SELECT * from Assignment"));

?>

<form method="POST">
<table>
<tr>
<td>İsim<span style="color:#FF7A1D;">*</span></td>
<td><input type="text" name="ad" value="" size="15" maxlength="15" /></td>
</tr>
<tr>
<td>Soyisim<span style="color:#FF7A1D;">*</span></td>
<td><input type="text" name="soyad" value="" size="15" maxlength="15" /></td>
</tr>
<tr>
<td>ID<span style="color:#FF7A1D;">*</span></td>
<td><input type="text" name="id" value="" size="15" maxlength="7" /></td>
</tr>
<td>Assignment Number<span style="color:#FF7A1D;">*</span></td>
<td><input type="text" name="assignment" value="" size="15" maxlength="5" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name = "submit1" value="Gönder" /></td>
</tr>

</form>
<form method="POST">
<table>

<tr>
<td>Data<span style="color:#FF7A1D;">*</span></td>
<td><select name = "tablelist" value ="">
  <option name="first "></option>
  <option name = "firstdata" value="<?php $resultt = $myDBClass->get_results("SELECT * from Assignment ");
echo $resultt[0]->ID;
echo "--";
echo $resultt[0]->Name;
echo "--";
echo $resultt[0]->Surname; 
echo "--";
echo $resultt[0]->assignmentNumber;

?>"><?php $resultt = $myDBClass->get_results("SELECT * from Assignment ");
echo $resultt[0]->ID;
echo "--";
echo $resultt[0]->Name;
echo "--";
echo $resultt[0]->Surname; 
echo "--";
echo $resultt[0]->assignmentNumber;

?> </option>
  <option value="saab"><?php $resultt = $myDBClass->get_results("SELECT * from Assignment ");
echo $resultt[1]->ID;
echo "--";
echo $resultt[1]->Name;
echo "--";
echo $resultt[1]->Surname; 
echo "--";
echo $resultt[1]->assignmentNumber;

?> </option>
  <option value="mercedes"><?php $resultt = $myDBClass->get_results("SELECT * from Assignment ");
echo $resultt[2]->ID;
echo "--";
echo $resultt[2]->Name;
echo "--";
echo $resultt[2]->Surname; 
echo "--";
echo $resultt[2]->assignmentNumber;

?> </option>
  <option value="audi"><?php $resultt = $myDBClass->get_results("SELECT * from Assignment ");
echo $resultt[3]->ID;
echo "--";
echo $resultt[3]->Name;
echo "--";
echo $resultt[3]->Surname; 
echo "--";
echo $resultt[3]->assignmentNumber;

?> </option>
  </td>
</select>
</tr>
<tr>

<td>İsim<span style="color:#FF7A1D;">*</span></td>
<td><input type="text" name="ad2" value="" size="15" maxlength="15" /></td>
</tr>
<tr>
<td>Soyisim<span style="color:#FF7A1D;">*</span></td>
<td><input type="text" name="soyad2" value="" size="15" maxlength="15" /></td>
</tr>
<tr>
<td>ID<span style="color:#FF7A1D;">*</span></td>
<td><input type="text" name="id2" value="" size="15" maxlength="7" /></td>
</tr>
<tr>
<td>Assignment Number<span style="color:#FF7A1D;">*</span></td>
<td><input type="text" name="assignment2" value="" size="15" maxlength="5" /></td>
</tr>
<tr>
<td colspan="1"><input type="submit" name="Update" value="Update" /></td>
</tr>
</form>

 

 

 

I have this code.

Link to comment
Share on other sites

ok I managed to update list item.

 

here is the code

<td><select name = "tablelist" value ="">
  <option name="first "></option>
<?php
$resultt = $myDBClass->get_results("SELECT * from Assignment ");
$sayi = 0;
while (($resultt[$sayi]->Name)!= null)
{
    echo " <option value='val'>";
    
echo $resultt[$sayi]->ID;
echo '--';
echo $resultt[$sayi]->Name;
echo '--';
echo $resultt[$sayi]->Surname; 
echo '--';
echo $resultt[$sayi]->assignmentNumber; 
echo "</option> ";

$sayi++;

}

?>
  </td>
</select>

 

now the thing I want is that

 

when I select a list item I want all the form is filled with the data from the table immediately. if I want to edit some of the data and clicked the update button it will be updated.

 

I did the update section but I have to enter the data by myself. I want all the data should be in the form when I select a list.

 

Please someone help me!!!

Link to comment
Share on other sites

So you want a dropdown list with all the rows.

 

<form method="post" action="myEditPage.php" />
<select name="id" />
<?php
$resultt = $myDBClass->get_results("SELECT * from Assignment ");
$sayi = 0;
while (($resultt[$sayi]->Name)!= null)
{
    echo ' <option value="'.$resultt[$sayi]->ID.'">'.$resultt[$sayi]->Name.'--'.$resultt[$sayi]->Surname.'</option>';

}
?>
</select>
</form>

 

now on your edit page you can query the table and get the existing data

$id = mysql_real_escape_string($_POST['id']);
$resultt = $myDBClass->get_results("SELECT * from Assignment where ID = '$id'");

 

Now you can set the values in your form

<input name="name" value="<?php echo $resultt[0]->Name; ?>" />

 

Not sure how your class is working but I think you get the idea.

 

Ray

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.