[英]While loop and PHP/MYSQL insert statement
Am I able or how can I create a while loop to run through some insert statemtns. 我是否可以或如何创建一个while循环来运行某些插入状态。 I have some code below that I have tried to use using i++ but I keep getting errors in relation to where I have put $i such as:
我下面有一些我尝试使用i ++使用的代码,但是与$ i的放置位置有关的错误不断,例如:
Object of class PDOStatement could not be converted to string
PDOStatement类的对象无法转换为字符串
Its hard to explain the previous code that amounts to this insert setup, but basically I decalre some variables and then they get bind and inserted with the following code but I have like 30 thins that could be inserted hence the while loop. 很难解释构成该插入设置的先前代码,但是基本上我对一些变量进行了缩放,然后使用下面的代码对它们进行了绑定和插入,但是我想插入30个Thin,因此可以使用while循环。 So the outcome should be supplier_name2 - supplier_name30 and same for address etc etc.
因此结果应为Supplier_name2-Supplier_name30,地址等应相同。
while ($i <= 30):
//LINE TWO
$suppliername = $this->supplier_name.$i;
if(!empty($suppliername)){
$sql0.$i = "INSERT INTO suppliers (supplier_name, supplier_address)
VALUES (:supplier_name.$i, :supplier_address.$i) ";
$st0.$i = $conn->prepare ( $sql0.$i );
$st0.$i->bindValue( ":supplier_name".$i, $this->supplier_name.$i, PDO::PARAM_STR );
$st0.$i->bindValue( ":supplier_address".$i, $this->supplier_address.$i, PDO::PARAM_STR);
$st0.$i->execute();
$this->id = $conn->lastInsertId();
$supplierid.$i = $this->id = $conn->lastInsertId();
}
$i++;
endwhile;
Any help appreciated. 任何帮助表示赞赏。 Ian
伊恩
Expression $sql0.$i
will be interpreted as string so PHP will try to use your PDOStatement
as string. 表达式
$sql0.$i
将被解释为字符串,因此PHP将尝试将您的PDOStatement
用作字符串。 It's not the way you should use prepared statements
. 这不是您应该使用
prepared statements
。
Move your prepared statement
before loop with some modifications. 在进行一些修改之前,将
prepared statement
移到循环之前。
$sql = "INSERT INTO suppliers (supplier_name, supplier_address)
VALUES (:supplier_name, :supplier_address) ";
$statement = $conn->prepare ( $sql );
Then perform bindValue()
and execute()
on $statement
variable. 然后对
$statement
变量执行bindValue()
和execute()
。 Idea is to declared once and use few times. 想法只声明一次,几次使用。 Also don't use
$i
in attributes names, it's unnecessary. 也不要在属性名称中使用
$i
,这是不必要的。
In > "supplier_name.$i"
the dot is literal. 在>
"supplier_name.$i"
,点是文字。 So it becomes "supplier_name.1", "supplier_name.2" etc 因此它变为“ supplier_name.1”,“ supplier_name.2”等
In ":supplier_address".$i
the dot is not literal, it is used as the concatenation operator. 在
":supplier_address".$i
不是文字,它用作连接运算符。 Hence you get "supplier_name1" and "supplier_name2" etc. Hence no replacement occurs I assume. 因此,您将获得“ supplier_name1”和“ supplier_name2”等。因此,我认为没有替换发生。
Can you try this, below one insert supplier_name1 - supplier_name30
in tables. 您可以在表中的一个以下插入
supplier_name1 - supplier_name30
下尝试吗?
while ($i <= 30):
//LINE TWO
$suppliername = $this->supplier_name.$i;
$supplier_address = $this->supplier_address.$i;
if(!empty($suppliername)){
$sql0 = "INSERT INTO suppliers (supplier_name, supplier_address)
VALUES (:supplier_name, :supplier_address) ";
$st0 = $conn->prepare ( $sql0 );
$st0->bindValue( ":supplier_name", $suppliername, PDO::PARAM_STR );
$st0->bindValue( ":supplier_address", $supplier_address, PDO::PARAM_STR);
$st0.$i->execute();
$this->id = $conn->lastInsertId();
$supplierid = $this->id = $conn->lastInsertId();
}
$i++;
endwhile;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.