首页 \ 问答 \ 使用mysqli_num_row错误检查重复项(Checking for Duplicates, using mysqli_num_row Error)

使用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
更新时间:2023-09-25 08:09

最满意答案

您可以使用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:

output

相关问答

更多
  • 问题: 您正在使用FlowLayout,即使您正确地执行它也不会尊重绝对定位。 setAlignmentX(...)用于向容器建议组件沿x轴的对齐方式。 如上所述,它取浮点数从0.0f到1.0f,其中0f表示向左对齐,0.5f居中,1.0f向右。 你肯定不想使用这种方法,或者它在这里是对应的。 绝对定位需要一个使用空布局的容器,而不是FlowLayout。 绝对定位通过setBounds(...)或setLocation(...)方法完成。 话虽如此,我建议你不要试图绝对定位,除非有绝对的需要,而且你很有可 ...
  • 您可以使用SpringLayout 。 这很棘手,但它会做到这一点。 SpringLayout springLayout = new SpringLayout(); Container cont = getContentPane(); cont.setLayout(springLayout); JLabel label = new JLabel("New label"); springLayout.putConstraint(SpringLayout.WEST, label, 20, SpringLayo ...
  • 确保你有 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中,场景坐标的默认原点位于屏幕的左下角。 因此,如果您希望标签位于顶角,则应继续使用屏幕高度作为标签的位置,同时使用0或屏幕宽度作为x位置。 根据侧面将其与左侧或右侧的水平对齐模式相结合,您的标签将位于正确的位置。 对于左上角的标签: label.horizontalAlignmentMode = .Left label.position = CGPoint(x:0.0, y:self.size.height) 或者右上角: label.horizontalAlignmentMode ...
  • 通过为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 ...
  • 在添加所有组件之前,您正在调用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 ...
  • 在这里疯狂猜测,但我想你想要的东西: 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已经有了一个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 ...
  • 首先看一下如何使用标签和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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。