如何在同一个表中选择where子句但是传递给表的字符串在同一个文本框中但具有不同的id

时间:2020-12-31 00:51:43

guy's i want to ask about selecting with where clause. the where clause i use $kode=$_POST['kode_mat'] and i call it in sql query into kode='$kode'. nah the problem is.. when i have a string that need to pass into the sql query is same, like i use $kode but the $_POST['kode_mat'] has different id like kode_mat1,kode_mat2,kode_mat3,kode_mat4,kode_mat5. and i want to pass it into sql query $sql ="SELECT * FROM material WHERE kode='$kode' ";. how to do it?

伙计我想问一下选择where子句。 where子句我使用$ kode = $ _ POST ['kode_mat'],我在sql查询中将其称为kode ='$ kode'。问题是..当我有一个需要传递给sql查询的字符串是相同的,就像我使用$ kode但$ _POST ['kode_mat']有不同的id,如kode_mat1,kode_mat2,kode_mat3,kode_mat4,kode_mat5。我想把它传递给sql查询$ sql =“SELECT * FROM material WHERE kode ='$ kode'”;;怎么做?

i have try to separate the php function into 5 php file. and i think that's make my directory has many file that has been saved. i use this code for the 5 php file

我试图将PHP功能分成5个php文件。我认为这使我的目录有许多已保存的文件。我使用此代码为5 php文件

<?php
include("../../Connections/koneksi.php");

$kode=$_POST['kode_mat']; // the variable for pass string i just change $_POST['']; ['kode_mat1'],['kode_mat2'],['kode_mat3']....

// Data for Titik1
$sql ="SELECT * FROM material WHERE kode='$kode' "; // and the query still same just change the id's of the textbox that i need to pass the string

$query = mysqli_query($db,$sql);
$rows = array();

while($tmp= mysqli_fetch_assoc($query)) {
    $rows[] = $tmp;
}

echo json_encode($rows);
mysqli_close($db);
?> 

this code above has no problem. it show the right value that i need. but i want to try it to make my separate php fuction can be called as one php. i has to try two code like this but it not work.

上面的代码没有问题。它显示了我需要的正确价值。但我想尝试它使我单独的PHP功能可以被称为一个PHP。我必须尝试这样的两个代码,但它不起作用。

1st code i have try

第一个代码我试过

<?php
include("../../Connections/koneksi.php");

$kode=$_POST['kode_mat'];
$kode=$_POST['kode_mat1'];
$kode=$_POST['kode_mat2'];
$kode=$_POST['kode_mat3'];
$kode=$_POST['kode_mat4'];
$kode=$_POST['kode_mat5'];
// Data for Titik1
$sql ="SELECT * FROM material WHERE kode='$kode' ";
$query = mysqli_query($db,$sql);
$rows = array();

while($tmp= mysqli_fetch_assoc($query)) {
    $rows[] = $tmp;
}

echo json_encode($rows);
mysqli_close($db);
?> 

and the second code i have try

和我试过的第二个代码

<?php
include("../../Connections/koneksi.php");

$kode=$_POST['kode_mat'];
$kode1=$_POST['kode_mat1'];
$kode2=$_POST['kode_mat2'];
$kode3=$_POST['kode_mat3'];
$kode4=$_POST['kode_mat4'];
$kode5=$_POST['kode_mat5'];
// Data for Titik1
$sql ="SELECT * FROM material WHERE kode='$kode' OR kode='$kode1' OR kode='$kode2' OR kode='$kode3' OR kode='$kode4' OR kode='$kode5'";
$query = mysqli_query($db,$sql);
$rows = array();

while($tmp= mysqli_fetch_assoc($query)) {
    $rows[] = $tmp;
}

echo json_encode($rows);
mysqli_close($db);
?> 

1 个解决方案

#1


1  

You can use if and else condition here for your solution like below.

您可以在此处使用if和else条件,如下所示。

<?php
include("../../Connections/koneksi.php");

if(isset($_POST['kode_mat']) && $_POST['kode_mat'] != "") {
$kode=$_POST['kode_mat'];
} else if(isset($_POST['kode_mat1']) && $_POST['kode_mat] != "") {
$kode=$_POST['kode_mat1'];
} else if(isset($_POST['kode_mat2']) && $_POST['kode_mat2'] != "") {
$kode=$_POST['kode_mat2'];
} else if(isset($_POST['kode_mat3']) && $_POST['kode_mat3'] != "") {
$kode=$_POST['kode_mat3'];
} else if(isset($_POST['kode_mat4']) && $_POST['kode_mat4'] != "") {
$kode4=$_POST['kode_mat4'];
} else {
$kode=$_POST['kode_mat5'];
}
// Data for Titik1
$sql ="SELECT * FROM material WHERE kode='$kode' ";
$query = mysqli_query($db,$sql);
$rows = array();

while($tmp= mysqli_fetch_assoc($query)) {
    $rows[] = $tmp;
}

echo json_encode($rows);
mysqli_close($db);
?> 

#1


1  

You can use if and else condition here for your solution like below.

您可以在此处使用if和else条件,如下所示。

<?php
include("../../Connections/koneksi.php");

if(isset($_POST['kode_mat']) && $_POST['kode_mat'] != "") {
$kode=$_POST['kode_mat'];
} else if(isset($_POST['kode_mat1']) && $_POST['kode_mat] != "") {
$kode=$_POST['kode_mat1'];
} else if(isset($_POST['kode_mat2']) && $_POST['kode_mat2'] != "") {
$kode=$_POST['kode_mat2'];
} else if(isset($_POST['kode_mat3']) && $_POST['kode_mat3'] != "") {
$kode=$_POST['kode_mat3'];
} else if(isset($_POST['kode_mat4']) && $_POST['kode_mat4'] != "") {
$kode4=$_POST['kode_mat4'];
} else {
$kode=$_POST['kode_mat5'];
}
// Data for Titik1
$sql ="SELECT * FROM material WHERE kode='$kode' ";
$query = mysqli_query($db,$sql);
$rows = array();

while($tmp= mysqli_fetch_assoc($query)) {
    $rows[] = $tmp;
}

echo json_encode($rows);
mysqli_close($db);
?>