How would you go about inserting multiple rows into a db if certain fields contained data? Consider the following:

register_case = mysql_query ("INSERT INTO cases VALUES ('$case','$p_firstname','$p_lastname','$city'),('$case','$p2_firstname','$p2_lastname','$city'),('$case','$p3_firstname','$p3_lastname','$city')");

This creates multiple rows with different first and last names but the case and city remain the same. However, in the event that there is only 1 entry, a row is still created for the other 2. How is it possible to create a row only if there is data present in a given field?

    nasablast wrote:

    How is it possible to create a row only if there is data present in a given field?

    By not telling MySQL to add a row for that data.

    In other words, use conditional statements in your PHP code to only append the 2nd, 3rd, ... nth set of values onto the SQL query if the necessary data is present/nonempty/whatever.

      Thanks for the reply. That is exactly what I am trying to do. I guess my real question is how do I do this? Could anyone provide a code example?

        Well first off, where are these values coming from? Whenever you start getting a bunch of variables with numbers added to them to form a series, it's almost always easier/more efficient to use arrays instead.

        EDIT: Also, please do not post multiple threads for the same issue.

          Write a Reply...