0

I am trying to insert data into mySQL query from PHP code. The code executes without any error but does not insert the data into the database. What could be wrong? Thanks in advance.

    <?php 
    include 'dbc.php';
    $sql="INSERT INTO `realtorl_leads`.`data` (`LeadFirstName`, `LeadLastName`, `LeadEmail`, `LeadEmail2`, `LeadPhone`, `LeadCity`, `LeadAddress`, `LeadPostcode`, `LeadUserId`, `LeadLeadStatusId`, `LeadMonth`, `LeadAreas`, `LeadMinPrice`, `LeadMaxPrice`, `LeadMinBedrooms`, `LeadMaxBedrooms`, `LeadMinBathrooms`, `LeadMaxBathrooms`, `LeadMinYear`, `LeadNextFollowup_mm`, `LeadNextFollowup_dd`, `LeadNextFollowup`) VALUES ('x', 'y', '[email protected]', '[email protected]', '3242342', 'karachi', '3234asdasd.karachi', '74600', '222', '32432', '23', '232', '232', '23', '232', '323', '323', '232', '23232', '3232', '232', '23232')";
    ?>

EDIT:

I have the following code now, the executed code just shows a new line and text saying it does not work. Please guide.

<?php 
include 'dbc.php';
$LeadFirstName = $_POST['LeadFirstName'];
$LeadFirstName = $_POST['LeadFirstName'];
$LeadEmail =$_POST['LeadEmail'];
$LeadEmail2 =$_POST['LeadEmail2'];
$LeadPhone =$_POST['LeadPhone'];
$LeadCity =$_POST['LeadCity'];
$LeadAddress =$_POST['LeadAddress'];
$LeadPostcode =$_POST['LeadPostcode'];
$LeadUserId =$_POST['LeadUserId'];
$LeadLeadStatusId =$_POST['LeadLeadStatusId'];
$LeadMonth =$_POST['LeadMonth'];
$LeadAreas =$_POST['LeadAreas'];
$LeadMinPrice =$_POST['LeadMinPrice'];
$LeadMaxPrice =$_POST['LeadMaxPrice'];
$LeadMinBedrooms =$_POST['LeadMinBedrooms'];
$LeadMaxBedrooms =$_POST['LeadMaxBedrooms'];
$LeadMinBathrooms =$_POST['LeadMinBathrooms'];
$LeadMaxBathrooms =$_POST['LeadMaxBathrooms'];
$LeadMinYear =$_POST['LeadMinYear'];
$LeadNextFollowup_mm =$_POST['LeadNextFollowup_mm'];
$LeadNextFollowup_dd =$_POST['LeadNextFollowup_dd'];
$LeadNextFollowup =$_POST['LeadNextFollowup'];
$sql="INSERT INTO 'realtorl_leads'.`data (`LeadFirstName`, `LeadLastName`, `LeadEmail`, `LeadEmail2`, `LeadPhone`, `LeadCity`, `LeadAddress`, `LeadPostcode`, `LeadUserId`, `LeadLeadStatusId`, `LeadMonth`, `LeadAreas`, `LeadMinPrice`, `LeadMaxPrice`, `LeadMinBedrooms`, `LeadMaxBedrooms`, `LeadMinBathrooms`, `LeadMaxBathrooms`, `LeadMinYear`, `LeadNextFollowup_mm`, `LeadNextFollowup_dd`, `LeadNextFollowup`) VALUES ($LeadFirstName, $LeadLastName, $LeadEmail, $LeadEmail2, $LeadPhone, $LeadCity, $LeadAddress, $LeadPostcode, $LeadUserId, $LeadLeadStatusId, $LeadMonth, $LeadAreas, $LeadMinPrice, $LeadMaxPrice, $LeadMinBedrooms, $LeadMaxBedrooms, $LeadNextFollowup_mm, $LeadNextFollowup_dd, $LeadNextFollowup)";
$result = mysql_query($sql);
echo($result."<br>");
if (mysql_affected_rows($result)){
  echo("worked");
}else {
  echo("does not work");
}
?>
5
  • 4
    Wheres the execution code? Commented Sep 1, 2013 at 3:59
  • 1
    Are you executing the query? Commented Sep 1, 2013 at 4:01
  • 1
    @ØHankyPankyØ: Oops.. Commented Sep 1, 2013 at 4:06
  • Query strings don't execute themselves. Show what's in dbc.php. Commented Sep 1, 2013 at 4:06
  • Avoid deprecated methods and enclose strings 9the stuff in VALUES) in inverted commas. Commented Sep 1, 2013 at 10:32

2 Answers 2

1

you will need to execute the query

include 'dbc.php';
$sql="INSERT INTO `realtorl_leads`.`data` (`LeadFirstName`, `LeadLastName`, `LeadEmail`, `LeadEmail2`, `LeadPhone`, `LeadCity`, `LeadAddress`, `LeadPostcode`, `LeadUserId`, `LeadLeadStatusId`, `LeadMonth`, `LeadAreas`, `LeadMinPrice`, `LeadMaxPrice`, `LeadMinBedrooms`, `LeadMaxBedrooms`, `LeadMinBathrooms`, `LeadMaxBathrooms`, `LeadMinYear`, `LeadNextFollowup_mm`, `LeadNextFollowup_dd`, `LeadNextFollowup`) VALUES ('Fahad', 'Uddin', 'fahad.fu@gmail,com', '[email protected]', '3242342', 'karachi', '3234asdasd.karachi', '74600', '222', '32432', '23', '232', '232', '23', '232', '323', '323', '232', '23232', '3232', '232', '23232')";
mysqli_query($con, $sql);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

1$ that dbc.php use mysql_ functions :)
Hits his head on the wall thrice and says sorry.
1

Your code has a part missing, try:

include 'dbc.php';
$sql="INSERT INTO `realtorl_leads`.`data` (`LeadFirstName`, `LeadLastName`, `LeadEmail`, `LeadEmail2`, `LeadPhone`, `LeadCity`, `LeadAddress`, `LeadPostcode`, `LeadUserId`, `LeadLeadStatusId`, `LeadMonth`, `LeadAreas`, `LeadMinPrice`, `LeadMaxPrice`, `LeadMinBedrooms`, `LeadMaxBedrooms`, `LeadMinBathrooms`, `LeadMaxBathrooms`, `LeadMinYear`, `LeadNextFollowup_mm`, `LeadNextFollowup_dd`, `LeadNextFollowup`) VALUES ('Fahad', 'Uddin', 'fahad.fu@gmail,com', '[email protected]', '3242342', 'karachi', '3234asdasd.karachi', '74600', '222', '32432', '23', '232', '232', '23', '232', '323', '323', '232', '23232', '3232', '232', '23232')";
$result = mysql_query($sql);

use $result to check whether your command is executing successfully, to do this try:

if (mysql_num_rows($result)){
  //echo successfull execution message
}else {

  //echo error message, it could be "information is not added"
}

though mysql is depreciated but it still works for me.

2 Comments

though mysql is depreciated but it still works for me. It still works and will always work for everyone who doesn't update his PHP to the latest version for any reason, still this might not be the best advise for any future code cause OP might one day decide to update even if you don't and then they will face this trouble. Why not move to mysqli, its almost similar
@Nauman: From PHP.net:mysql_num_rows Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().