MySQL Forums
Forum List  »  PHP

Why can't i connect to my sql database?
Posted by: Jacdonmi Bawo
Date: June 23, 2018 04:16AM

Please i have tried severally to connect to a my sql database on cpanel

I used this codes

The form code is
<form action= "insert.php" method="post">


First name: <input type="text" name="fname" id="fname"/><br><br>

Last name: <input type="text" name="lname"id="lname" /><br><br>

Gender:<input type="text" name="Gender" id="Gender"/><br><br>

<p><label>Select State:</label><select name="State"
id="State">

<option value="-"selected="selected">-</option>
<option value="Abia">Abia</option>
<option value="Adamawa">Adamawa</option>
<option value="Akwaibom">Akwaibom</option>
<option value="Anambra">Anambra</option>



</select></label></p>

Lga: <input type="text" name="Lga" id="Lga"/><br><br>







<input type="submit" name="s1"id="s1"value="Submit" />

</form>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

</form>

The php script in cgi folder is insert.php


<?php


$host="localhost",

$user_name="admin",

$pwd="^^^^^^",

$database_name="Data",





$db = mysql_connect("$localhost,$lamsanng_admin ,$^^^^^^);

if (!$db)

{

die('Could not connect: ' . mysql_error());

}



mysql_select_db($lamsanng_Data ", $db);
$Id=$_POST["Id"];,
$fname = $_POST["fname"];,
$fname= $_POST["fname"];
$Gender= $_POST["Gender"];
$Lga= $_POST["Lga"];
$State= $_POST["State"];
$submitdate=date("Ymd");



$query="INSERT INTO 'tbl_Jobs'";

( 'Id', 'Firstname', 'Lastname','Gender','State','Lga','submit_date')VALUES { ( '$First_name',$'Last_name,'$Gender','$State,'$Lga,'$submit_date');



$result=mysql_query($query);


if (!mysql_query($sql,$db))

{

die('Error: ' . mysql_error());

}


echo "1 record added";



mysql_close($db)

?>





The code to retrieve the stored data to my webpage is



<?php
// create short variable names
$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);

if (!$searchtype || !$searchterm) {
echo 'You have not entered search details. Please go back and try again.';
exit;
}

if (!get_magic_quotes_gpc()){
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}

@ $db = new mysqli('localhost', 'lamsanng_admin', '^^^^^^', 'lamsanng_Data');

if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

$query = "select * from 'tbl_Jobs' where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);

$num_results = $result->num_rows;

echo "<p>records found: ".$num_results."</p>";

for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong>".($i+1).". Fname: ";
echo htmlspecialchars(stripslashes($row['Fname']));
echo "</strong><br />Lname: ";
echo stripslashes($row['Id']);
echo "</p>";
}

$result->free();
$db->close();

?>




Please what is wrong with the scripts.
Thank you.^^^^^^^
is hidden password

Options: ReplyQuote


Subject
Written By
Posted
Why can't i connect to my sql database?
June 23, 2018 04:16AM


Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.