Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hey, last week me and one of my professors created a code to do an insert, giving an option for the user chose where to insert, and I want to use this code for the rest of the functions...
I don't have much time, please help.

CODE:

<?php
$self = $_SERVER['PHP_SELF'];
$host_name = "127.0.0.1";
$database = "visteon";
$username = "root";          
$password = "";          
$Tabela ="";

try {
$dbo = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}

// select table to insert data

if(!isset($_POST['submeterTabela']) && !isset($_POST['submeterDados'])) {
	
	$form ="<form action=\"$self\" method=\"post\">";
	
		$result = $dbo->query("SHOW TABLES");
	$form.= "<select name='Tabela' class='select_box'>";
		while ($row = $result->fetch(PDO::FETCH_NUM)) {
	$form.= "<option value='$row[0]'>$row[0]</option>";
}
	$form.= "</select>";
	$form.="<br><input type=\"submit\" name=\"submeterTabela\" value=\"Submeter\"class='button'>";
		echo($form);
}

// input data

if(isset($_POST['submeterTabela']) && !isset($_POST['submeterDados']))
{
  $Tabela=$_POST['Tabela'];
  $form ="<form action=\"$self\" method=\"post\">";
  $form.="<input type=\"hidden\" name=\"Tabela\"class='tabela1'value=\"$Tabela\"> <br>";  
  $form.="Introduz Dados<br>";  
  $sql ="SHOW columns from ".$Tabela;
  $result = $dbo->query($sql);
  while ($row = $result->fetch(PDO::FETCH_NUM)) {
   $sql1="select Campo from descritivoscampos where Tabela='".$Tabela."' and Campo='".$row[0]."';";
   $Resultado = $dbo->query($sql1);
   $linha = $Resultado->fetch(PDO::FETCH_NUM);  
   $form .= "$linha[0]";
   $form.="<input type=\"text\" name=\"$row[0]\" size=\"5\"><br>";   
  }
  $form.=" <br><input type=\"submit\" name=\"submeterDados\"class='botao'value=\"Submeter\">";
  echo($form);
}

if(isset($_POST['submeterDados']))
{
  $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("Data added successfully !!");
  }	



}
?>


What I have tried:

I have tried this and nothing happened:

if(isset($_POST['submeterDados']))
{
  $Tabela=$_POST['Tabela'];
  $sql ="SHOW columns from ".$Tabela;
  $result = $dbo->query($sql);
  $sql1 = "DELETE FROM'".$Tabela."' WHERE id = $Id";
}
Posted
Updated 22-Apr-19 11:32am
v2
Comments
Richard MacCutchan 22-Apr-19 9:44am    
"I don't have much time, please help."
So what exactly do you want us to do?
#realJSOP 23-Apr-19 13:36pm    
He should have started sooner.
Member 14197794 24-Apr-19 3:07am    
I've begun this almost a month ago, i just came here because I'm stuck. This is one of my internship project's
Member 14197794 22-Apr-19 9:48am    
Help me or show me how to adapt it
[no name] 22-Apr-19 10:00am    
Suck up to the prof some more.

1 solution

Don't leave your homework to the last minute.

Your example doesn't work because you don't run the query, which it seems is done by this code:

$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("Data added successfully !!");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900