marcus_epik Posted July 4, 2012 Share Posted July 4, 2012 Hi, I am trying to create a chat board for my website so people can comment on pages. Basically what the code should do is insert the name of the person who is commenting, insert the actual comment, insert page name so it knows what page it refers to and date. For some reason I am getting this error when i try to submit on the form below: Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/95/9446795/html/tutorial.php on line 202 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/95/9446795/html/tutorial.php on line 202 ff34784d-d826-4e47-a7f8-a7c020083793 Y2:ff34784d-d826-4e47-a7f8-a7c020083793 this is the code: <?php //connect to my sql ?> <?php // This file is www.developphp.com curriculum material // Written by Adam Khoury January 01, 2011 // http://www.youtube.com/view_play_list?p=442E340A42191003 // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?php // Check to see the URL variable is set and that it exists in the database if (isset($_GET['id'])) { // Connect to the MySQL database $id = preg_replace('#[^0-9]#i', '', $_GET['id']); // Use this var to check to see if this ID exists, if yes then get the product // details, if no then exit this script and give message why $sql = mysql_query("SELECT * FROM tutorials WHERE id='$id' LIMIT 1"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { // get all the product details while($row = mysql_fetch_array($sql)){ $name = $row["name"]; $uploader = $row["uploader"]; $w1n = $row["website_1_name"]; $w1a = $row["website_1_address"]; $w1r = $row["website_1_review"]; $w2n = $row["website_2_name"]; $w2a = $row["website_2_address"]; $w2r = $row["website_2_review"]; $w3n = $row["website_3_name"]; $w3a = $row["website_3_address"]; $w3r = $row["website_3_review"]; $date_added = strftime("%b %d, %Y", strtotime($row["upload_date"])); $sql = mysql_query("SELECT * FROM tutorials WHERE id='$id' "); $productCount = mysql_num_rows($sql); while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["name"]; $category = $row["subject"]; $date_added = strftime("%b %d, %Y", strtotime($row["upload_date"])); $product_list = "Tutorial ID: $id - <strong>$product_name</strong> - $category - <em>Added $date_added</em> <a href='edittutorials.php?id=$id'>edit</a> • <a href='managetutorials.php?deleteid=$id'>delete</a><br />"; } } } // This block grabs the whole list for viewing // This block grabs the whole list for viewing $comments_list = ""; $sql = mysql_query("SELECT * FROM comments WHERE tutorial='$name'"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { while($row = mysql_fetch_array($sql)){ $by = $row["by"]; $comment = $row["comment"]; $date_added = strftime("%b %d, %Y, at %T", strtotime($row["time"])); $comments_list .= "<hr /><br /><b>$by</b>: $comment <br /><span style='color:grey'>$date_added</span>"; } } else { $comments_list = "There are currently no comments on this tutorial. You can add a comment below."; } } else { echo "Data to render this page is missing."; exit(); } mysql_close(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> <!-- #BeginTemplate "master.dwt" --> <head> <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" /> <title>How Net - <?php echo $name ?></title> <meta name="description" content="free website template" /> <meta name="keywords" content="enter your keywords here" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="css/style.css" /> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/jquery.easing.min.js"></script> <script type="text/javascript" src="js/jquery.lavalamp.min.js"></script> <script type="text/javascript"> $(function() { $("#lava_menu").lavaLamp({ fx: "backout", speed: 700 }); }); </script> <script type="text/javascript" src="js/jquery.nivo.slider.pack.js"></script> <script type="text/javascript"> $(window).load(function() { $('#slider').nivoSlider(); }); </script> <style type="text/css"> .auto-style1 { margin-left: 0; margin-right: 629px; } </style> </head> <body> <div id="header"> <div> <a href="index.php"> <img src="images/LogoColorTextRight.jpeg" alt="" height="99" width="365" class="auto-style1" /></a></div> </div><!--close header--> <div id="main"> <div id="site_content"> <div id="menubar"> <ul class="lavaLampWithImage" id="lava_menu"> <li><a href="index.php">Home</a></li> <li><a href="web_design.php">Web Design</a></li> <li><a href="diy.php">DIY</a></li> <li><a href="cooking.php">Cooking</a></li> <li><a href="programming.php">Programming</a></li> <li><a href="computers.php">Computers</a></li> <li><a href="user_register.php">Register</a></li> <li><a href="user_login.php">Login</a></li> <li><a href="forums.php">Forum</a></li> </ul> </div><!--close menubar--> <div id="content"> <div class="content_item" style="width: 760px"> <!-- #BeginEditable "content" --> <h2> </h2> <h2><?php echo $name ?></h2> <table style="width: 100%"> <tr> <td style="width: 211px" valign="top"><?php echo "<a href='$w1a'> $w1n </a>"?></td> <td rowspan="2"><?php echo $w1r ?></td> </tr> <tr> <td style="width: 211px"> </td> </tr> </table> <hr /> <table style="width: 100%"> <tr> <td style="width: 212px" valign="top"><?php echo "<a href='$w2a'> $w2n </a>"?></td> <td rowspan="2"><?php echo $w2r ?></td> </tr> <tr> <td style="width: 212px"> </td> </tr> </table> <hr /> <table style="width: 100%"> <tr> <td style="width: 213px" valign="top"><?php echo "<a href='$w3a'> $w3n </a>"?></td> <td rowspan="2"><?php echo $w3r ?></td> </tr> <tr> <td style="width: 213px"> </td> </tr> </table> <br /> <hr /> <br /> <h1>Comments:</h1> <br /> <?php echo $comments_list ?> <br /> <hr /> <h1>Add a comment:</h1> <br /> <?php // Parse the form data and add inventory item to the system if (isset($_POST['comment'])) { $by = $_POST['Name']; $comment = $_POST['comment']; // See if that product name is an identical match to another product in the system // Add this product into the database now mysql_query("INSERT INTO comments (by, comment) VALUES('$by','$comment'"); } ?> <form action="tutorial.php?id=<?php echo $id ?>" method="post"> <table style="width: 100%"> <tr> <td style="width: 80px">Name:</td> <td><input name="Name" type="text" /> </td> </tr> <tr> <td style="width: 80px">Comment:</td> <td> <textarea name="comment" style="width: 320px; height: 155px"></textarea> </td> </tr> </table> <input name="Submit1" type="submit" value="submit" /><input name="Reset1" type="reset" value="reset" /> </form> <!-- #EndEditable --> </div><!--close content_item--> <div class="sidebar_container" style="width: 120px"> <div class="sidebar" style=" float:right; width: 120px"> <div class="sidebar_item" style="width: 92px"> <script type="text/javascript"><!-- google_ad_client = "ca-pub-7476366512115748"; /* right ad unit */ google_ad_slot = "3889569307"; google_ad_width = 120; google_ad_height = 600; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div><!--close sidebar_item--> </div><!--close sidebar_item--> </div><!--close sidebar--> </div><!--close sidebar_container--> <br style="clear:both;" /> </div><!--close content--> </div><!--close site_content--> <div id="footer"> <a href="login.php">Admin Login</a> | Copywrite © How Net 2012. All rights reserved | design: <a href="http://www.araynordesign.co.uk">ARaynorDesign</a> </div><!--close footer--> </body> <!-- #EndTemplate --> </html> this is the code that doesn't work. Here is some code in the same diretory connecting to the same database but a different table and works: <?php //connect to mysql ?> <?php if (!isset($_SESSION["admin"])) { header("location: login.php"); exit(); } ?> <?php // Run the actual connection here mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql"); mysql_select_db("$db_name") or die ("no database"); // Be sure to check that this manager SESSION value is in fact in the database $managerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]); // filter everything but numbers and letters $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["admin"]); // filter everything but numbers and letters $password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]); // filter everything but numbers and letters // Run mySQL query to be sure that this person is an admin and that their password session var equals the database information $sql = mysql_query("SELECT * FROM admin WHERE id='$managerID' AND username='$manager' AND password='$password' LIMIT 1"); // query the person // ------- MAKE SURE PERSON EXISTS IN DATABASE --------- $existCount = mysql_num_rows($sql); // count the row nums if ($existCount == 0) { // evaluate the count echo "Your login session data is not on record in the database."; exit(); } ?> <?php // Parse the form data and add inventory item to the system if (isset($_POST['name'])) { $tutorial_name = mysql_real_escape_string($_POST['name']); $keywords = mysql_real_escape_string($_POST['keywords']); $category = mysql_real_escape_string($_POST['category']); $w1n = mysql_real_escape_string($_POST['w1n']); $w1a = mysql_real_escape_string($_POST['w1a']); $w1r = mysql_real_escape_string($_POST['w1r']); $w2n = mysql_real_escape_string($_POST['w2n']); $w2a = mysql_real_escape_string($_POST['w2a']); $w2r = mysql_real_escape_string($_POST['w2r']); $w3n = mysql_real_escape_string($_POST['w3n']); $w3a = mysql_real_escape_string($_POST['w3a']); $w3r = mysql_real_escape_string($_POST['w3r']); // See if that product name is an identical match to another product in the system $sql = mysql_query("SELECT id FROM tutorials WHERE name='$tutorial_name' LIMIT 1"); $productMatch = mysql_num_rows($sql); // count the output amount if ($productMatch > 0) { echo 'Sorry you tried to place a duplicate tutorial name into the system, <a href="managetutorials.php">click here</a>'; exit(); } // Add this product into the database now $sql = mysql_query("INSERT INTO tutorials (uploader, name, subject, key_words, website_1_name, website_1_address, website_1_review, website_2_name, website_2_address, website_2_review, website_3_name, website_3_address, website_3_review, upload_date) VALUES('admin','$tutorial_name','$category','$keywords','$w1n','$w1a', '$w1r', '$w2n','$w2a', '$w2r', '$w3n','$w3a', '$w3r', now())") or die (mysql_error()); $pid = mysql_insert_id(); // Place image in the folder $newname = "$pid.jpg"; move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname"); header("location: managetutorials.php"); exit(); } ?> <?php // This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM tutorials"); $productCount = mysql_num_rows($sql); // count the output amount if (1 == 1) { while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["name"]; $category = $row["subject"]; $date_added = strftime("%b %d, %Y", strtotime($row["upload_date"])); $product_list .= "Tutorial ID: $id - <strong>$product_name</strong> - $category - <em>Added $date_added</em> <a href='edittutorials.php?id=$id'>edit</a> • <a href='managetutorials.php?deleteid=$id'>delete</a><br />"; } } else { $product_list = "You have no products listed in your store yet"; } ?> <?php // Delete Item Question to Admin, and Delete Product if they choose if (isset($_GET['deleteid'])) { echo 'Do you really want to delete tutorial with ID of ' . $_GET['deleteid'] . '? <a href="managetutorials.php?yesdelete=' . $_GET['deleteid'] . '">Yes</a> | <a href="inventory_list.php">No</a>'; exit(); } if (isset($_GET['yesdelete'])) { // remove item from system and delete its picture // delete from database $id_to_delete = $_GET['yesdelete']; $sql = mysql_query("DELETE FROM tutorials WHERE id='$id_to_delete' LIMIT 1") or die (mysql_error()); // unlink the image from server // Remove The Pic ------------------------------------------- $pictodelete = ("../inventory_images/$id_to_delete.jpg"); if (file_exists($pictodelete)) { unlink($pictodelete); } header("location: managetutorials.php"); exit(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> <!-- #BeginTemplate "master.dwt" --> <head> <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" /> <title>ARaynorDesign Template</title> <meta name="description" content="free website template" /> <meta name="keywords" content="enter your keywords here" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="css/style.css" /> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/jquery.easing.min.js"></script> <script type="text/javascript" src="js/jquery.lavalamp.min.js"></script> <script type="text/javascript"> $(function() { $("#lava_menu").lavaLamp({ fx: "backout", speed: 700 }); }); </script> <script type="text/javascript" src="js/jquery.nivo.slider.pack.js"></script> <script type="text/javascript"> $(window).load(function() { $('#slider').nivoSlider(); }); </script> <style type="text/css"> .auto-style1 { margin-left: 0; margin-right: 629px; } </style> </head> <body> <div id="header"> <div> <a href="index.php"> <img src="images/LogoColorTextRight.jpeg" alt="" height="99" width="365" class="auto-style1" /></a></div> </div><!--close header--> <div id="main"> <div id="site_content"> <div id="menubar"> <ul class="lavaLampWithImage" id="lava_menu"> <li><a href="index.php">Home</a></li> <li><a href="web_design.php">Web Design</a></li> <li><a href="diy.php">DIY</a></li> <li><a href="cooking.php">Cooking</a></li> <li><a href="programming.php">Programming</a></li> <li><a href="computers.php">Computers</a></li> <li><a href="user_register.php">Register</a></li> <li><a href="user_login.php">Login</a></li> <li><a href="forums.php">Forum</a></li> </ul> </div><!--close menubar--> <div id="content"> <div class="content_item" style="width: 760px"> <!-- #BeginEditable "content" --> <a href="managetutorials.php#form"><span class="auto-style1">↓ Add a new tutorial ↓</span></a> <br/> <?php echo $product_list; ?> <hr /> <a name="form"></a> <form action="" method="post"> <table style="width: 100%"> <tr> <td>name</td> <td><input name="name" type="text"> </td> </tr> <tr> <td style="height: 29px">subject</td> <td style="height: 29px"><select name="category" style="width: 126px"> <option></option> <option value="Web_Design">Web Design</option> <option>DIY</option> <option>Cooking</option> <option>Programming</option> <option>Computers</option> </select> </td> </tr> <tr> <td valign="top">keywords<br><br>(new line for each <br> one)</td> <td> <textarea name="keywords" style="width: 253px; height: 213px"></textarea> </td> </tr> <tr> <td>website 1 name</td> <td><input name="w1n" type="text"></td> </tr> <tr> <td>website 1 address</td> <td><input name="w1a" type="text"></td> </tr> <tr> <td style="height: 26px">website 1 review</td> <td style="height: 26px"> <textarea name="w1r" style="width: 250px; height: 160px"></textarea></td> </tr> <tr> <td>website 1 logo</td> <td> <input type="file" name="fileField" id="fileField" /> </td> </tr> <tr> <td style="height: 22px">website 2 name</td> <td style="height: 22px"> <input name="w2n" type="text"></td> </tr> <tr> <td>website 2 address</td> <td><input name="w2a" type="text"></td> </tr> <tr> <td>website 2 review</td> <td> <textarea name="w2r" style="width: 250px; height: 160px"></textarea></td> </tr> <tr> <td>website 2 logo</td> <td> <input name="w2l" style="width: 222px" type="file"></td> </tr> <tr> <td>website 3 name</td> <td><input name="w3n" type="text"></td> </tr> <tr> <td>website 3 address</td> <td><input name="w3a" type="text"></td> </tr> <tr> <td>website 3 review</td> <td><input name="w3r" type="text"><br> <textarea name="w3r" style="width: 250px; height: 160px"></textarea></td> </tr> <tr> <td>website 3 logo</td> <td> <input name="w3l" style="width: 222px" type="file"></td> </tr> </table> <input name="Submit1" type="submit" value="submit"><input name="Reset1" type="reset" value="reset"></form> <!-- #EndEditable --> </div><!--close content_item--> <div class="sidebar_container" style="width: 120px"> <div class="sidebar" style=" float:right; width: 120px"> <div class="sidebar_item" style="width: 92px"> <script type="text/javascript"><!-- google_ad_client = "ca-pub-7476366512115748"; /* right ad unit */ google_ad_slot = "3889569307"; google_ad_width = 120; google_ad_height = 600; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div><!--close sidebar_item--> </div><!--close sidebar_item--> </div><!--close sidebar--> </div><!--close sidebar_container--> <br style="clear:both;" /> </div><!--close content--> </div><!--close site_content--> <div id="footer"> <a href="login.php">Admin Login</a> | Copywrite © How Net 2012. All rights reserved | design: <a href="http://www.araynordesign.co.uk">ARaynorDesign</a> </div><!--close footer--> </body> <!-- #EndTemplate --> </html> MOD EDIT: . . . BBCode tags added. Quote Link to comment https://forums.phpfreaks.com/topic/265204-chat-board-code-not-working/ Share on other sites More sharing options...
haku Posted July 4, 2012 Share Posted July 4, 2012 I'm suspecting that a connection cannot be made to the mysql server on line 202. Check your database connection information. Quote Link to comment https://forums.phpfreaks.com/topic/265204-chat-board-code-not-working/#findComment-1359131 Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2012 Share Posted July 4, 2012 When posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/265204-chat-board-code-not-working/#findComment-1359132 Share on other sites More sharing options...
xyph Posted July 4, 2012 Share Posted July 4, 2012 Wow. Use the [ic] [/ic] tags please! You're attempted to perform a MySQL query without connecting to a database. Your [m]mysql_connect[/m] call is half way down the page. Meanwhile, you have 4 queries above it. Quote Link to comment https://forums.phpfreaks.com/topic/265204-chat-board-code-not-working/#findComment-1359133 Share on other sites More sharing options...
marcus_epik Posted July 4, 2012 Author Share Posted July 4, 2012 sorry, i am new to this forum and thanks i will give that a try Quote Link to comment https://forums.phpfreaks.com/topic/265204-chat-board-code-not-working/#findComment-1359137 Share on other sites More sharing options...
marcus_epik Posted July 4, 2012 Author Share Posted July 4, 2012 I'm suspecting that a connection cannot be made to the mysql server on line 202. Check your database connection information. I have checked and checked again and have moved the mysql_connect() command further up the and that was not the code that was not working, that was the code that was working It is also able to read properly from the same table in the same page thanks in advance marcus Quote Link to comment https://forums.phpfreaks.com/topic/265204-chat-board-code-not-working/#findComment-1359138 Share on other sites More sharing options...
darkfreaks Posted July 4, 2012 Share Posted July 4, 2012 change: mysql_query("INSERT INTO comments (by, comment) VALUES('$by','$comment'"); to: mysql_query("INSERT INTO comments (`by`,`comment`) VALUES('$by','$comment')"); explanation: you have white space in your syntax which needs to be removed furthermore BY is a SQL reserved word. so i put back ticks around the values. Quote Link to comment https://forums.phpfreaks.com/topic/265204-chat-board-code-not-working/#findComment-1359160 Share on other sites More sharing options...
PFMaBiSmAd Posted July 4, 2012 Share Posted July 4, 2012 You have a mysql_close statement at line 72 in the code. Every mysql_ statement after that point will fail. Remove the mysql_close statement, it's not needed since the connection will be closed when the execution of the page ends. Quote Link to comment https://forums.phpfreaks.com/topic/265204-chat-board-code-not-working/#findComment-1359172 Share on other sites More sharing options...
marcus_epik Posted July 4, 2012 Author Share Posted July 4, 2012 You have a mysql_close statement at line 72 in the code. Every mysql_ statement after that point will fail. Remove the mysql_close statement, it's not needed since the connection will be closed when the execution of the page ends. Thanks, I cant believe i made such a silly mistake. The code now works thankyou Quote Link to comment https://forums.phpfreaks.com/topic/265204-chat-board-code-not-working/#findComment-1359193 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.