Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 74735

how to solve a problem retrieving data from select tag in PHP

$
0
0

I took a few hours to search if anyone had a similar issue, but it seems no. Im using a select tag to create option in which table in DB should i store info. Usually it works, this time it refuses to work.

<form method="post" action="" enctype='multipart/form-data'>
    <div class="selectDB">
        Select DB :
        <br>
        <select name="table2" id="1">
            <option value="trainingpics">Training</option>
            <option value="wallpics">Wallpaper</option>
            <option value="communitypics">Community</option>
            <option value="eventpics">Event</option>
            <option value="bapics">BeforeAndAfter</option>
        </select>
    </div>

this is how define it!

if (isset($_POST['table2'])) {
    $sql = "SELECT  id FROM " . $_POST['table2'];
    $lastID = 0;
    $firstID = 1;
    $first_cnt = 0;
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        // output data of each row
        while ($row = $result->fetch_assoc()) {
            $lastID = $row['id'];
            if ($first_cnt == 0) {
                $firstID = $row['id'];
                $first_cnt++;
            }
        }
    }

//End of get Last ID

//Delete Functions --
    for ($x = $lastID; $x >= $firstID; $x--) {
        $var1 = "Del" . $x;
        echo $var1 . "<br>";
        if (isset($_POST[$var1])) {
            echo "----";
            $sql = "DELETE FROM " . $_POST['table2'] . " WHERE id=" . $x;

            if ($conn->query($sql) === TRUE) {
                echo "Record deleted successfully";
            } else {
                echo "Error deleting record: " . $conn->error;
            }
        }
    }
}

and this is the code im using to process. I tried different ways(probably 30 different ways), but i do not know why the php always returns the select as NOT SET. Any ideas ?

This is the entire code , its divided in 2 parts : 1/

<?php
$servername = "localhost";
$username = "root";
 $password = "";
$db = "gallery";
$printRes = "";
 // Create connection
  $conn = new mysqli($servername, $username, $password,$db);

// Check connection
 if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}
 // Delete BLOCK
 if(isset($_POST['but_upload'])){

 $name = $_FILES['file']['name'];
 $target_dir = "upload/";
 $target_file = $target_dir . basename($_FILES["file"]["name"]);

// Select file type
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Valid file extensions
$extensions_arr = array("jpg","jpeg","png","gif");
$tbl = $_POST['table'];
//Get last ID
$sql = "SELECT  id FROM ".$_POST['table'];
$lastID = 0;
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            $lastID = $row['id'];
        }
    }
   //End of get Last ID

   // Check extension
   if( in_array($imageFileType,$extensions_arr) ){

      // Insert record
     $lastID++;
     $query = "INSERT into ".$tbl."(id,Photo) 
     VALUES(".$lastID.",'".$name."')";
      if ($conn->query($query) === TRUE) {
      $printRes="New record created successfully";
    } else {
    $printRes= "Error: " . $query . "<br>" . $conn->error;
   }

    // Upload file
   move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name);

 }

}
function ShowInfo()
 {
   $showP = "";
   if(isset($_POST['ShowInfo']))
    {
     $servername = "localhost";
     $username = "root";
     $password = "";
     $db = "gallery";
     $bname = "Del";
     $conn = new mysqli($servername, $username, $password,$db);

     // Check connection
    if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
     }        
     $sql = "SELECT  id,Photo FROM ".$_POST['table2'];
     $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            $showP .=  "<div class = 'ResDB1'>
            <h3>'".$row['id']."'</h3>
            <img src='"."upload/".$row["Photo"]."'>
            <h4>'".$row['Photo']."'</h4>
            <input type='submit' name = '".$bname.$row['id']."' class = 'DelButton'>
            </div>";
        }
    } else {
        $showP="";
    }
   }
  if(isset($_POST['ResetData']))
   {$showP="";}
   echo $showP ; 
 }

  $conn->close();
  ?>
  and 2/

<?php
function Del()
{
$servername = "localhost";
$username = "root";
$password = "";
$db = "gallery";
$printRes = "";
// Create connection
$conn = new mysqli($servername, $username, $password,$db);

// Check connection
if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
}
// Delete BLOCK
//Get last ID
 if(isset($_POST['table2']))
{
$sql = "SELECT  id FROM ".$_POST['table2'];
$lastID = 0;
$firstID = 1;
$first_cnt = 0;
  $result = $conn->query($sql);
  if ($result->num_rows > 0) {
      // output data of each row
      while($row = $result->fetch_assoc()) {
          $lastID = $row['id'];
          if($first_cnt==0)
          {
              $firstID = $row['id'];
              $first_cnt++;
          }
      }
  }

//End of get Last ID

//Delete Functions --
for($x = $lastID; $x>=$firstID; $x--)
{ 
 $var1 = "Del".$x;
 echo $var1."<br>";
 if(isset($_POST[$var1]))
  {
     echo "----";
    $sql = "DELETE FROM ".$_POST['table2']." WHERE id=".$x;

   if ($conn->query($sql) === TRUE) {
       echo "Record deleted successfully";
   } else {
       echo "Error deleting record: " . $conn->error;
   }          
 }
 }
 }
 $conn->close();
 }
  ?>

THE HTML !

<div class="manage partAction">
  <h2>Manage Content</h2>
   <div id="outerB">
  <form method="post" action="" enctype='multipart/form-data'>
   <div class="selectDB">
   Select DB : 
   <br>
   <select name="table2" id="1">
    <option value="trainingpics" selected>Training</option>
    <option value="wallpics">Wallpaper</option>
     <option value="communitypics">Community</option>
     <option value="eventpics">Event</option>
    <option value="bapics">BeforeAndAfter</option>
   </select> 
   </div>
   <div id= "sub">
    <input type='submit' value='ShowInfo' name='ShowInfo' id="shinfo">
    <br>
    <input type='submit' value='ResetData' name='ResetData' id="reset">
  </div> 
  </form>
  <br>
  <div id="resDB">
  <form method="post" action="#" enctype='multipart/form-data'>
  <?php ShowInfo(); ?>
  <?php Del();?>
  </form>
  </div>

Working part of select into FORM

<div class="upload partAction">
    <h2>Upload Content</h2>
     <div id="outerA">
 <form method="post" action="" enctype='multipart/form-data'>
  <div id="file"><input type='file' name='file'/></div>
  <div class="selectDB">
  Select DB : 
  <br>
  <select name="table" id="">
    <option value="trainingpics">Training</option>
    <option value="wallpics">Wallpaper</option>
    <option value="communitypics">Community</option>
   <option value="eventpics">Event</option>
   <option value="bapics">BeforeAndAfter</option>
  </select> 
  </div>
  <div id= "sub">
    <input type='submit' value='UploadData' name='but_upload' id="upload">
  </div>
    <div id="result"><?php echo $printRes; ?></div>
 </form>
 </div>
</div>

Viewing all articles
Browse latest Browse all 74735

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>