How to adapt the insert code?

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
gsilva18
New php-forum User
New php-forum User
Posts: 1
Joined: Thu May 02, 2019 2:13 am

Thu May 02, 2019 2:23 am

Hey, I need your help guys, i've been in almost php forums and no one could help me.
Me and one of my teacher's built this code to input data inside DB, in the end of the class I asked him if it is possible to adapt this code to read/delete/update and he said YES! .. after almost 2weeks i cant do it, please help me
.....

Code: Select all

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

  $Tabela=$_POST['Tabela']; 
  $sql ="SHOW columns from ".$Tabela;
  $result = $dbo->query($sql);
  $sql1 = "insert into $Tabela(";
  $aux=0;
  while ($row = $result->fetch(PDO::FETCH_NUM)) {
   if ($aux!=0) $sql1 .=", ";
   if ($row[0]!='id')
   {
    $sql1 .="$row[0]";  
    $aux=1; 
   }
  }
  $sql1 .=") values (";

  $result = $dbo->query($sql);
  $aux=0;
  while ($row = $result->fetch(PDO::FETCH_NUM)) {
   if ($aux!=0) $sql1 .= ", ";

   if ($row[0]!='id')
   {
	   $aa=$_POST[$row[0]];
	   $sql1 .="'$aa'";  
//$sql1 .="$row[0]";  
    $aux=1; 
  }
  }
  $sql1 .=");";
  $conn = mysqli_connect($host_name, $username, $password, $database)
	or die("Could not connect.");
  $result = mysqli_query($conn, $sql1)
	or die("Could not execute SQL query");
 if ($result) {     
	echo("Informação introduzida com Sucesso!!");
  }	
  }

?>

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 831
Joined: Mon Feb 22, 2016 5:52 pm

Thu May 02, 2019 11:16 am

Change the SQL, that's all you need to do to change the operation.

That is one mash up of code?

Post Reply