使用mysqli_num_row错误检查重复项(Checking for Duplicates, using mysqli_num_row Error)
我正在为项目构建一个Inventory页面,我试图检查,当我尝试插入一个新项目时,如果它已经在数据库中。 我遇到了mysqli_num_rows函数的一个问题,它要求一个参数。 我很确定我的查询是事先工作所以我不明白为什么它这样做。 是因为查询返回0行吗? 以下是代码。
另外我使用mysqli_connect的include,所以它不在下面的代码中。
<?php if(isset($_POST["product_name"])){ $product_name = mysqli_real_escape_string($link,$_POST["product_name"]); $price = mysqli_real_escape_string($link,$_POST["price"]); $category = mysqli_real_escape_string($link,$_POST["category"]); $subcategory = mysqli_real_escape_string($link,$_POST["subcategory"]); $details = mysqli_real_escape_string($link,$_POST["details"]); $sql = mysqli_query($link,"SELECT id FROM products WHERE product_name = '$product_name'"); if(mysql_num_rows($sql) > 0){ echo "sorry you tried to add a duplicate <a href = Inventory_list.php> Click here </a>"; exit(); } $sql = mysqli_query($link, "INSERT INTO products (product_name,price,details,category,subcategory,date_added) VALUES('$product_name','$price','$details','$category', '$subcategory',now())") or die("Update Error: ".mysqli_error()); $pid = mysqli_insert_id(); $newname = $pid.jpg; move_uploaded_file($_FILES['fileField']['tmp_name'],"../Images/$newname"); } ?>
I am building an Inventory page for a project and I am trying to check, when I attempt to insert a new item if its already in the database. I am running into a problem with the mysqli_num_rows function in such a way that it asks for a parameter. I'm pretty sure my query beforehand is working so I dont understand why its doing this. Is it because the query returns 0 rows? Below is the code.
Also I am using an include for the mysqli_connect so its not in the code below.
<?php if(isset($_POST["product_name"])){ $product_name = mysqli_real_escape_string($link,$_POST["product_name"]); $price = mysqli_real_escape_string($link,$_POST["price"]); $category = mysqli_real_escape_string($link,$_POST["category"]); $subcategory = mysqli_real_escape_string($link,$_POST["subcategory"]); $details = mysqli_real_escape_string($link,$_POST["details"]); $sql = mysqli_query($link,"SELECT id FROM products WHERE product_name = '$product_name'"); if(mysql_num_rows($sql) > 0){ echo "sorry you tried to add a duplicate <a href = Inventory_list.php> Click here </a>"; exit(); } $sql = mysqli_query($link, "INSERT INTO products (product_name,price,details,category,subcategory,date_added) VALUES('$product_name','$price','$details','$category', '$subcategory',now())") or die("Update Error: ".mysqli_error()); $pid = mysqli_insert_id(); $newname = $pid.jpg; move_uploaded_file($_FILES['fileField']['tmp_name'],"../Images/$newname"); } ?>
原文:https://stackoverflow.com/questions/26864983
最满意答案
您可以使用
SpringLayout
。这很棘手,但它会做到这一点。
SpringLayout springLayout = new SpringLayout(); Container cont = getContentPane(); cont.setLayout(springLayout); JLabel label = new JLabel("New label"); springLayout.putConstraint(SpringLayout.WEST, label, 20, SpringLayout.WEST, cont); springLayout.putConstraint(SpringLayout.SOUTH, label, -10, SpringLayout.SOUTH, cont); cont.add(lblNewLabel);
弹簧布局允许您设置组件侧面和框架之间的距离,并最终以动态方式设置它的位置(当您调整大小时,该位置将保持相对于框架的位置)
这是输出:
You can use a
SpringLayout
.It's tricky, but it'll do the trick.
SpringLayout springLayout = new SpringLayout(); Container cont = getContentPane(); cont.setLayout(springLayout); JLabel label = new JLabel("New label"); springLayout.putConstraint(SpringLayout.WEST, label, 20, SpringLayout.WEST, cont); springLayout.putConstraint(SpringLayout.SOUTH, label, -10, SpringLayout.SOUTH, cont); cont.add(lblNewLabel);
Spring Layout allows you to set the distance between the sides of components and the frame, and eventually set it's location in a dynamic way (the location will remain relative to the frame when you resize)
this is the output:
相关问答
更多-
问题: 您正在使用FlowLayout,即使您正确地执行它也不会尊重绝对定位。 setAlignmentX(...)用于向容器建议组件沿x轴的对齐方式。 如上所述,它取浮点数从0.0f到1.0f,其中0f表示向左对齐,0.5f居中,1.0f向右。 你肯定不想使用这种方法,或者它在这里是对应的。 绝对定位需要一个使用空布局的容器,而不是FlowLayout。 绝对定位通过setBounds(...)或setLocation(...)方法完成。 话虽如此,我建议你不要试图绝对定位,除非有绝对的需要,而且你很有可 ...
-
将JLabel定位在角落里(Positioning JLabel in Corner)[2023-10-10]
您可以使用SpringLayout 。 这很棘手,但它会做到这一点。 SpringLayout springLayout = new SpringLayout(); Container cont = getContentPane(); cont.setLayout(springLayout); JLabel label = new JLabel("New label"); springLayout.putConstraint(SpringLayout.WEST, label, 20, SpringLayo ... -
如何将JLabel放在标题栏正下方的右上角?(How to place JLabel on top right corner just below the title bar?)[2023-08-26]
确保你有 panel.setLayout(null); 如果您希望自己定位组件。 但是,随着应用程序复杂性的增加,识别和使用适合您需求的布局管理器会使事情变得更容易。 Make sure you have panel.setLayout(null); if you wish to position the components yourself. But identifying and using a layout manager that would fit your needs would make ... -
角落里的Sprite Kit标签(Sprite Kit label in the corner)[2022-09-17]
在Sprite Kit中,场景坐标的默认原点位于屏幕的左下角。 因此,如果您希望标签位于顶角,则应继续使用屏幕高度作为标签的位置,同时使用0或屏幕宽度作为x位置。 根据侧面将其与左侧或右侧的水平对齐模式相结合,您的标签将位于正确的位置。 对于左上角的标签: label.horizontalAlignmentMode = .Left label.position = CGPoint(x:0.0, y:self.size.height) 或者右上角: label.horizontalAlignmentMode ... -
Java帮助:JLabel(Java help: JLabel's)[2022-11-30]
通过为JFrame指定不同的布局到FlowLayout() BorderLayout ,可以实现所需的结果,例如, setLayout (new BorderLayout()); // asign layout to JFrame add(label1,BorderLayout.PAGE_START); //Add JLabel 1 to Jframe add(label2,BorderLayout.CENTER); //Add JLabel 2 to Jframe name = new JLabe ... -
JLabel没有出现(JLabel doesnt appear)[2021-09-30]
在添加所有组件之前,您正在调用setVisible(true) 。 不要这样做。 该orde是: 添加您的组件 呼叫pack() 调用setVisible(true) 如果稍后你做了更改,那么在容器上调用revalidate() ,然后repaint() You're calling setVisible(true) before adding all your components. Don't do this. The orde is: add your components call pack() c ... -
JLabel,GridLayout(JLabel, GridLayout)[2023-11-06]
在这里疯狂猜测,但我想你想要的东西: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Gui extends JFrame { private JFrame window = new JFrame(); private JButton but[] = new JButton[9]; public Gui() { window.setSize(300 ... -
创建自己的JLabel(Creation own JLabel)[2024-02-22]
JLabel已经有了一个getX和getY方法,用于将标签放在屏幕上,你已经(无意中)覆盖了这个功能,现在返回不相关的信息。 它不适用于x和y位置。 它是用于hashmap的id,我认为它很有用;) 因此,我建议创建一个ID类,它包含您需要的信息(可能会覆盖equals和hashcode方法,以便更容易进行比较),而是使用它(提供getID和setID方法),而不是使用getX/Y JLabel already has a getX and getY method which is to position ... -
JLabel和Image?(JLabel with Image?)[2022-02-20]
首先看一下如何使用标签和JavaDocs来实现`javax.swing.JLabel 基本上,你有很多选择。 您可以使用 JLabel label = new JLabel("This is an Icon", icon, JLabel.CENTER); 您可以使用 JLabel label = new JLabel(); label.setText("This is an icon"); label.setIcon(icon); 您可以使用 JLabel label = new JLabel("Thi ... -
Swing使用布局管理器来放置组件。 您必须了解他们如何有效地使用它们。 您可以将布局管理器设置为null,并自行进行布局,但不建议这样做,因为您每次都必须跟踪新组件,并在窗口移动缩小时执行自己的布局计算等。 布局管理员起初有点难以掌握。 你的窗户可能是这样的: 就像这个一样简单http://img827.imageshack.us/img827/7043/capturadepantalla201007z.png 使用此代码: import javax.swing.*; import java.awt.Fo ...