Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:46:21+00:00 2026-06-02T20:46:21+00:00

I am having issues inserting this into my database I think tis when I

  • 0

I am having issues inserting this into my database I think tis when I try to convert my prdocutPrice string or stock string into decimal and int values. I am pretty certain I am doing the rest right can someone confirm for me?

<?php 

    if (isset($_POST['addSubmitted'])) {

        $errors = array();
        require_once ('mysql_connect.php');

            //This gets all the other information from the form 
            $name=$_POST['productName']; 
            $description=$_POST['productDescription']; 
            $price= floatval($_POST['productPrice']); 
            $stock= intval($_POST['productStock']);


        if (empty($errors)) {
            //Writes the information to the database 
            mysql_query("INSERT INTO products (name, description, price, stock) VALUES ($name, $description, $price, $stock)"); 
            $result = mysql_query($query);

            if (mysql_affected_rows() == 1) {
                // Show thank you message
                echo '<span style="color:green;">Your product has been added.</span>';
            } else {
                echo '<font color="red">We were unable to add your product to the database.</font>';
            }

        } else {
            echo '<font color="red"><h3>Error!</h3>
            The following error(s) occured:<br /></font>';

            foreach ($errors as $msg) {
                echo " - <font color=\"red\">$msg</font><br />\n";
            }
        }
    }

?> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:46:22+00:00Added an answer on June 2, 2026 at 8:46 pm

    None of your string values are correctly quoted in the INSERT statement. Surround them in single quotes.

    Also, first thing’s first — be sure to call mysql_real_escape_string() on all the string input values, as they are currently vulnerable to SQL injection attacks.

    $name = mysql_real_escape_string($_POST['productName']); 
    $description= mysql_real_escape_string($_POST['productDescription']); 
    $price= floatval($_POST['productPrice']); 
    $stock= intval($_POST['productStock']);
    

    Further, you are calling mysql_query() twice instead of storing the SQL string into your variable $query.

    // Quote the string values,
    // store the SQL as a variable then pass it to mysql_query()
    $query = "INSERT INTO products (name, description, price, stock) VALUES ('$name', '$description', $price, $stock)"; 
    $result = mysql_query($query);
    

    A call to echo mysql_error(); would have helped to debug the problems with your SQL statement.

    Finally, one more note I’ll add – in addition to calling intval() or floatval() on strings passed from $_POST, it is usually a good idea to verify that the numbers are actually numbers. Otherwise if they are non numeric values, they will be cast to 0 and you’ll get zeros in your database when you probably shouldn’t have inserted it at all (since it is invalid data).

    if (is_numeric($_POST['productPrice'])) {
       $price = floatval($_POST['productPrice']);
    }
    else // non numeric value, don't do the insert with bad data
    

    For positive or zero integers, I like to use ctype_digit():

    if (ctype_digit($_POST['productStock'])) {
      $stock = intval($_POST['productStock']);
    }
    else // bad input value, don't do insert
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue with inserting JSON into a database, my intention is to
I am having issues with trying to convert an UTF-8 string to unicode. I
I am having an issue with inserting words with special characters into my database.
We're having issues inserting links into rich text in Sitecore 6.1.0. When a link
Hey guys. I'm having this silly issue with inserting text with forwardslash into cart
Having issues referencing $(this) from within a the nested ajax 'success' function... I know
Still having issues with this problem. Please help if you can. So I am
Im having issues getting this to work, maybe its not even possible? I have
I've recently written a custom API for inserting data into my database from multiple
Im having issues vertically positioning text inside of a text input field in Firefox.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.